C # unmanaged cross-thread Commission debugging,

Source: Internet
Author: User
Tags gdb debugger

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 ++.

 

Related Article

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.