C # unmanaged cross-thread delegate debugging

Source: Internet
Author: User
Tags gdb debugger

Call MinGW's so file using C #, display the video data back to the WPF interface, and register the callback function. C + + encountered a problem with the delegate being reclaimed when invoking the callback function, prompting: "The garbage collection delegate of type has made a callback." This can cause application crashes, corruption, and data loss. When you pass a delegate to unmanaged code, the managed application must keep the delegates active until they are sure that they will not be called again. "
This issue occurs after a program has been running for some time. The effect that you see from the GDB debugger is that all variables are normal and run to the callback function segmentation fault

The OpenCV imshow display image is directly called through C + +, and the mat used is reconstructed by pointers and size, proving that data preparation is no problem and should be a callback function call

Online to see People's solutions:

    • Defines a delegate as a member variable of static;
    • Define the callback function as static;
    • Allow GC to not reclaim delegated gc.keepalive (DISPLAY_CB);
    • Add tags before variables:
      • [ThreadStatic]
      • static public MINGW.DISPLAY_CB DISPLAY_CB;

However, these methods can only resolve a single-threaded call to the callback function, in which case the VS2010 debugger pops up the error message, saying "the type of garbage collection delegate has a callback." This can cause application crashes, corruption, and data loss. When you pass a delegate to unmanaged code, the managed application must keep the delegates active until they are sure that they will not be called again. ”
In the dynamic library, the C + + code opens another thread to call the C # callback function, and the program will only pop up this window prompt:

So now we can only temporarily use C # to establish a worker thread, to actively invoke C + + functions, in C + + through semaphore blocking way to implement event triggering.

C # unmanaged cross-thread delegate debugging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.