I am attempting to pass a managed function pointer void (*)(void *)
to my unmanaged library. My unmanaged library calls this callback with a pointer to a frame of data protected by a CriticalSection. While the managed callback is running, nothing else can modify the frame of data due to the Critical Section. However, I am getting Access Violations and Heap Corruptions just by entering the callback.
EDIT: I forgot to mention. The StartStreaming()
steals the thread it manages. Furthermore, it creates a separate thread for dispatching new data to the given callback. The callback is called in this separate thread.
So far I have done the follow:
//Start Streaming
streaming_thread_ = gcnew Thread(gcnew ThreadStart(&Form1::WorkerThreadFunc));
streaming_thread_->Start();
Where:
extern "C" {
#include "libavcodec\avcodec.h"
#include "libavutil\avutil.h"
}
namespace TEST_OCU {
delegate void myCallbackDelegate(void * usr_data); //Declare a delegate for my unmanaged code
public ref class Form1 : public System::Windows::Forms::Form
{
public:
static void WorkerThreadFunc()
{
myCallbackDelegate^ del = gcnew myCallbackDelegate(&Form1::frame_callback);
MessageBox::Show("Starting to Streaming", "Streaming Info");
if(rtsp_connection_ != NULL)
rtsp_connection_->StartStreaming();
//rtsp_connection_->StartStreaming((void (*)(void *)) System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(del).ToPointer() );
MessageBox::Show("Done Streaming", "Streaming Info");
}
static void __cdecl frame_callback(void * frame)
{
AVFrame * casted_frame = (AVFrame *)frame;
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
if(rtsp_connection_ == NULL)
rtsp_connection_ = new NeyaSystems::RTSPConnection("rtsp://url");
}
private: static RTSPConnection * rtsp_connection_ = NULL;
}
}
- I've omitted a lot of pointless code...
StartStreaming
defaults to a NULL pointer, in this case I get no corruptionStartStreaming
with the delegated function pointer causes heap corruptionRTSPConnection
is implemented in native C++ and contains C calls as well (libavcodec)RTSPConnection
contains two threads, communication and frame dispatch thread (calls the managed callback)
Could anyone give me a breadcrumb? Thank you so much in advance.