Fix possible GC'd function pointer in internal msg loop.

This commit is contained in:
Eugene Wang
2018-11-23 16:07:34 -05:00
parent b23757ebdc
commit b489a3ef58
2 changed files with 7 additions and 7 deletions

View File

@@ -20,10 +20,8 @@ namespace NTwain.Threading
static ushort classAtom;
static IntPtr hInstance;
static readonly uint dequeMsg = UnsafeNativeMethods.RegisterWindowMessageW("WinMsgLoopQueue");
const int CW_USEDEFAULT = -1;
static IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
// keep delegate around to prevent GC
static readonly WndProcDelegate WndProc = new WndProcDelegate((hWnd, msg, wParam, lParam) =>
{
Debug.WriteLine($"Dummy window got msg {(WindowMessage)msg}.");
switch ((WindowMessage)msg)
@@ -33,7 +31,9 @@ namespace NTwain.Threading
return IntPtr.Zero;
}
return UnsafeNativeMethods.DefWindowProcW(hWnd, msg, wParam, lParam);
}
});
const int CW_USEDEFAULT = -1;
static void InitGlobal()
{
@@ -45,7 +45,7 @@ namespace NTwain.Threading
wc.cbSize = Marshal.SizeOf(wc);
wc.style = ClassStyles.CS_VREDRAW | ClassStyles.CS_HREDRAW;
var procPtr = Marshal.GetFunctionPointerForDelegate(new WndProcDelegate(WndProc));
var procPtr = Marshal.GetFunctionPointerForDelegate(WndProc);
wc.lpfnWndProc = procPtr;
wc.cbClsExtra = 0;

View File

@@ -23,7 +23,7 @@ namespace NTwain
private IntPtr _hWnd;
// cache generated twain sources so if you get same source from same session it'll return the same object
readonly Dictionary<string, DataSource> _ownedSources = new Dictionary<string, DataSource>();
// need to keep delegate around to prevent GC?
// need to keep delegate around to prevent GC
readonly Callback32 _callback32Delegate;
// for windows only
readonly WinMsgLoop _winMsgLoop;