R
ropo
I have a managed wrapper class that has a delegate to be called from
unmanaged code:
public ref class CmmMessageCallbackWrapper
{
private:
delegate void InternalMessageDelegate( void*,
CmmService::Message::CMessage* );
public:
CmmMessageCallbackWrapper()
{
_InternalMessageCallback = gcnew
InternalMessageDelegate(this,&CmmMessageCallbackWrapper::InternalMessageCallback);
}
void* GetPtrCallback()
{
return
(void*)System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(_InternalMessageCallback);
}
private:
void InternalMessageCallback(void* pData,
CmmService::Message::CMessage* pMessageData)
{
return;
}
InternalMessageDelegate^ _InternalMessageCallback;
};
I then setup my unmanaged callback as follows:
bool CMyManagedClass::CallUnmanaged()
{
CmmMessageCallbackWrapper^ Wrapper = gcnew
CmmMessageCallbackWrapper();
FN_MESSAGE_CALLBACK pfnCallback = (FN_MESSAGE_CALLBACK)Wrapper-
}
GetPtrCallback() returns the marshalled function pointer. Problem is
in a release build the callback causes a crash, in debug it is fine.
The only thing I can think of is the 'Wrapper' is garbage collected
before the delegate is called.
Help and advice greatfully received. I'm stuck.
unmanaged code:
public ref class CmmMessageCallbackWrapper
{
private:
delegate void InternalMessageDelegate( void*,
CmmService::Message::CMessage* );
public:
CmmMessageCallbackWrapper()
{
_InternalMessageCallback = gcnew
InternalMessageDelegate(this,&CmmMessageCallbackWrapper::InternalMessageCallback);
}
void* GetPtrCallback()
{
return
(void*)System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(_InternalMessageCallback);
}
private:
void InternalMessageCallback(void* pData,
CmmService::Message::CMessage* pMessageData)
{
return;
}
InternalMessageDelegate^ _InternalMessageCallback;
};
I then setup my unmanaged callback as follows:
bool CMyManagedClass::CallUnmanaged()
{
CmmMessageCallbackWrapper^ Wrapper = gcnew
CmmMessageCallbackWrapper();
FN_MESSAGE_CALLBACK pfnCallback = (FN_MESSAGE_CALLBACK)Wrapper-
return _pUnmanagedObj->CallFuncWithCallback(pfnCallback);GetPtrCallback();
}
GetPtrCallback() returns the marshalled function pointer. Problem is
in a release build the callback causes a crash, in debug it is fine.
The only thing I can think of is the 'Wrapper' is garbage collected
before the delegate is called.
Help and advice greatfully received. I'm stuck.