我试图托管函数指针传递void (*)(void *)
我的非托管库。 我的非托管库调用这个回调的指针由CriticalSection的保护数据的帧。 虽然管理回调运行,没有别的可以修改数据的帧,由于临界区。 不过,我只是通过进入回调获得访问冲突和堆损坏。
编辑 :我忘了提。 该StartStreaming()
窃取其管理的线程。 此外,它创造了新的调度数据给定的回调一个单独的线程。 回调被称为在这个单独的线程。
到目前为止,我已经做了如下:
//Start Streaming
streaming_thread_ = gcnew Thread(gcnew ThreadStart(&Form1::WorkerThreadFunc));
streaming_thread_->Start();
哪里:
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;
}
}
- 我省略了很多无谓的代码...
-
StartStreaming
默认为一个空指针,在这种情况下,我没有得到任何腐败 -
StartStreaming
与委托函数指针导致堆损坏 -
RTSPConnection
在本地C ++实现,包含C调用以及(libavcodec的) -
RTSPConnection
包含两个线程,通信和帧调度线程(调用管理回调)
谁能给我一个面包屑? 非常感谢你提前。