I have been experimenting with C++/CLI delegates (as I am trying to make a .NET reference library), and I have been having the following problem.
I define a delegate in
void WriteMessage(void* Function, int number)
Passing function pointers as void* is a pretty bad idea. It prevents the compiler from checking you are doing anything wrong. There is something wrong, although the compiler cannot detect it in this specific case. The delegate is marshaled as a function pointer that uses the __stdcall calling convention, your actual function pointer uses the __cdecl calling convention, the default for native code. Which causes the stack to get imbalanced on the call.
You can fix it by applying the [UnmanagedFunctionPointer] attribute to the delegate declaration, specify CallingConvention::Cdecl.
A function pointer created from a delegate is invisible to the garbage collector, and isn't counted during reachability analysis.
From the documentation:
You must manually keep the delegate from being collected by the garbage collector from managed code. The garbage collector does not track reference [sic] to unmanaged code.
If the delegate is collected, the function pointer is left dangling, and your program will behave badly. An access violation is one of the more likely outcomes, but not the only possibility. If the memory that used to contain the native/managed trampoline is reused for some other data, the CPU could try to interpret it as instructions, which might mean just about anything.
The solution is to keep the delegate reachable, for example via the C++/CLI gcroot
class, which is a thin wrapper around .NET GCHandle
.