CallbackOnCollectedDelegate was detected

前端 未结 3 2028

I am subclassing an application. My subclassed Window procedure is within a DLL. My subclassing code inside the DLL looks somewhat like this (stripped down, removed other no

相关标签:
3条回答
  • 2020-12-06 05:21

    Call me crazy but storing a reference should resolve this:

     private static readonly WndProcDelegateType _reference = MyWndProc;  
    
    0 讨论(0)
  • 2020-12-06 05:23
    oldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, MyWndProc);
    

    That forces C# to create a delegate object on-the-fly. It translates the code to this:

    oldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, new WndProcDelegateType(MyWndProc));
    

    which is a problem, that delegate object isn't referenced anywhere. The next garbage collection is going to destroy it, pulling the rug out from under the unmanaged code. You already did the proper thing in your code, you just forgot to use it. Fix:

    oldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, newWndProc);
    

    Deriving your own class from NativeWindow and uses its AssignHandle() method is the better mousetrap btw. Call ReleaseHandle() when you see the WM_DESTROY message.

    0 讨论(0)
  • 2020-12-06 05:27

    the callback function can be invoked after the call returns, the managed caller must take steps to ensure that the delegate remains uncollected until the callback function finishes. For detailed information about preventing garbage collection, see Interop Marshaling with Platform Invoke.

    http://msdn.microsoft.com/en-us/library/eaw10et3.aspx

    0 讨论(0)
提交回复
热议问题