c# - Pin a function pointer -


I have to pass a managed callback to an unmanaged TCP receiver as its one thread needs to be in existence for the lifetime of the application So, I need to stop it from collecting garbage. I've read everywhere that function pointers do not have to pin and GCandDell. Elok will work to stop garbage collection.

But has it been given? I have noticed that the applepool crashes with this code of access violation.

It supports this fact.

Update: This has greatly reduced the crash, so I should not doubt the fact that this error occurs because function pointer was collected in the garbage?

Is there any problem with this approach?

  typed zero (__cdecl * ProcMessageFunc) (zero * param, zero * paramBuf, ULONG bufSize); FuncDelegate ^ fp = gcnew MessageFuncDelegate (this, and handler); Pin_ptr & lt; MessageFuncDelegate ^ & gt; PinnedFunctionPointer = & amp; Amp; Fp; Rate = receiver ((ProcMessageFunc) pinnedFunctionPointer);    

I do exactly what you recommend - GCHandle on representative .Alloc but no pinning with - and there is no problem in the widespread use on many different platforms and on NIT editions. 2.0 - 4. Something like this:

  Delgate Handle = GCIndale Olloc (xlldelegate); Function Pointer = Marshall Gatefunction Pointerfar Delegate (XLDAliate); With the   

function pointer , the original code for the subsequent cleaning and passed DelegateHandle

It seems the best reference ..

Unlike this reference, you need to protect a representative from garbage collection, just that does not require pinning .

Comments