I need to pass a managed callback to an unmanaged TCP receiver. Since this is a thread that must exist throughout the life of the application, I need to prevent it from garbage collection. I read everywhere that binding function pointers is not required, and GCHandle.Alloc will do the task of preventing garbage collection.
But is this given? I saw that the AppPool that hosts this code crashes with access violation. Why should I not suspect that this error occurs because the function pointer was garbage collected?
This post supports this fact.
Update: This appears to have significantly reduced the number of crashes. Is there a problem with this approach?
typedef void (__cdecl *ProcMessageFunc)(void* param, void* paramBuf, ULONG bufSize); FuncDelegate^ fp = gcnew MessageFuncDelegate(this, &Handler); pin_ptr<MessageFuncDelegate^> pinnedFunctionPointer = &fp; ret = Receiver ((ProcMessageFunc)pinnedFunctionPointer);
c # interop c ++ - cli
Krishter
source share