A
A
Avery0072014-05-14 16:30:13
C++ / C#
Avery007, 2014-05-14 16:30:13

How to create a reference to a delegate so that the garbage collector does not destroy it?

When resizing the application window, a delegate is called, which is passed to unmanaged code, and the program crashes with an error: A delegate of type "OPENGL_SDK!OPENGL_SDK.Glut+dReshapeFunc::Invoke" received by the garbage collector was called back. This can cause application crashes and data corruption or loss. When passing delegates to unmanaged code, a managed application must keep them alive until it is guaranteed that they will never be called again.
I can guess what needs to be done so that in managed code the delegate remains throughout the life of the program, but I do not know how to do this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AntonGoretskiy, 2014-05-14
@AntonGoretskiy

Keep a reference to this delegate in some global variable before passing it to unmanaged code.

A
Anton Martsen, 2014-05-17
@martsen

Declare a static field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question