CallbackOnCollectedDelegate was detected

邮差的信 提交于 2019-12-01 18:00:31

In RegisterWndClass, you're creating a local WNDCLASS instance - wc, the lpfnWndProc field holds a delegate which is used to call back into your code. As this is a local variable, as soon as that method completes, wc is eligible for garbage collection, as is the delegate that is being used to call back into your code.

You must ensure that the delegate you're passing as a callback is not garbage collected. This can be achieved by adding a static field to the MessageMonitor class:

private static readonly NativeMethods.WndProc StaticWndProcDelegate = WndProc;

And in RegisterWndClass where you currently have:

wc.lpfnWndProc = WndProc;

Replace it with:

wc.lpfnWndProc = StaticWndProcDelegate;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!