C # unmanaged cross-thread Commission debugging,
Use C # To Call The so file of mingw, return the video data to the wpf interface for display, and register the callback function. When calling the callback function, C ++ encounters the issue that the delegate is recycled and prompts:"The type of garbage collection delegate is called back. This may cause application crash, damage, and data loss. When delivering delegates to unmanaged code, managed applications must keep these delegates active until you are sure they will not be called again."
This problem occurs after the program runs normally for a period of time. The result shown in the gdb debugger is that all variables are normal and segmentation fault is used when the callback function is run.
Using c ++ to directly call the imshow of opencv to display images, and the Mat used is re-constructed by pointer and size, it proves that data preparation is normal, it should be a callback function call problem.
Solutions for viewing people on the Internet:
- Define the delegate as a static member variable;
- Define the callback function as static;
- Let the GC not recycle the delegate GC. KeepAlive (display_cb );
- Add a label before the variable:
- [ThreadStatic]
- Static public Mingw. DISPLAY_CB display_cb;
However, these methods can only solve the situation where a single thread calls the callback function. In this case, the VS2010 debugger will pop up an error message saying "the type of garbage collection delegate has been called back. This may cause application crash, damage, and data loss. When delivering delegates to unmanaged code, managed applications must keep these delegates active until you are sure they will not be called again ."
In the dynamic library, the C ++ code opens another thread to call the C # callback function. At this time, the program will only display this window prompt:
So now we can only temporarily use C # To create a working thread to actively call the C ++ function and implement event triggering through semaphore blocking in C ++.