at first, exuse if the following question is simple to solve, but i
normaly coding with C# and now have to use C++/CLI for one project.
My Problem is that i have to use a native c++ sdk to read pictures
from ip cameras. I get the native c++ decoder worked so far and now
try to send callbacks from unmanaged to managed code, to signal wenn a
picture is fully received.
To get a first view on that area i implemented a little test where i
extract a pointer from a delegate and sending the pointer to unmanaged
code. In the unmanaged function i use the callbackfunction (after
filling an array), jump back to to managed code and marshaling the
generated array to the managed world.
That little example is working fine in debugmode but as soon as i
running in releasemode the programm crashs and i dont know why.
So, at first my Code:
unsigned char * data;
void Caller(int size, void callback(int, unsigned char*))
data = new unsigned char[size];
for(int i = 0; i < size; i++)
data[i] = i;
using namespace System;
using namespace System::Runtime::InteropServices;
public delegate void callback(int, unsigned char*);
ref class mTest
array<unsigned char> ^destination;
callback ^d = gcnew callback(this,&mTest::callTest);
IntPtr ptr = Marshal::GetFunctionPointerForDelegate(d);
umTest *umT = new umTest();
static_cast<void (__cdecl *)(int, unsigned char*)>
void callTest(int size, unsigned char* test)
this->size = size;
destination = gcnew array<unsigned char>(size);
test = NULL;
for(int i = 0; i<size; i++)
When it runs in release mode, the callbackfunction is normally called
but after finishing the function, he is called again. On the
Marshal::Copy(...)-Line then the programm is crashing without any
I hope someone of you find the mistake, i have no idea anymore...
Thanks in advance