mirror of
https://github.com/soukoku/ntwain.git
synced 2025-11-24 08:47:06 +08:00
Fix possible GC'd function pointer in internal msg loop.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user