CsWin32: System.ExecutionEngineException

Actual behavior

I was running the app and watching process memory in VS diagnostic tools. Checking the value intermittently. The app had been running for a minute or two. When it crashed.

Output An unhandled exception of type ‘System.ExecutionEngineException’ occurred in Core.dll

Extracted embedded document “F:\Projects\CoreWindowProjects\CoreWindow\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.PInvoke.User32.g.cs” to “C:\Users\deads\AppData\Local\Temp.vsdbgsrc\C3EE4EF4AB5791F171B8ABE25BD66C50E0B23E07\Windows.Win32.PInvoke.User32.g.cs”

    Crash in C:\Users\deads\AppData\Local\Temp\.vsdbgsrc\C3EE4EF4AB5791F171B8ABE25BD66C50E0B23E07\Windows.Win32.PInvoke.User32.g.cs

    internal static unsafe winmdroot.Foundation.BOOL PeekMessage(out winmdroot.UI.WindowsAndMessaging.MSG lpMsg, winmdroot.Foundation.HWND hWnd, uint wMsgFilterMin, uint wMsgFilterMax, winmdroot.UI.WindowsAndMessaging.PEEK_MESSAGE_REMOVE_TYPE wRemoveMsg)
    {
    	fixed (winmdroot.UI.WindowsAndMessaging.MSG* lpMsgLocal = &lpMsg)
    	{
    		winmdroot.Foundation.BOOL __result = PInvoke.PeekMessage(lpMsgLocal, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg); ** Crash on this line.
    		return __result;
    	}
    }

System.ExecutionEngineException HResult=0x80131506 Source=<Cannot evaluate the exception source> StackTrace: <Cannot evaluate the exception stack trace>

Expected behavior

I expect to be able to watch the program run for several minutes and inspect process memory value

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

This is a collected Delegate issue. Using C# function pointers would be the recommended solution.

WNDCLASSEXW windowClass = new()
{
    cbSize = (uint)Unsafe.SizeOf<WNDCLASSEXW>(),
    style = style,
    lpfnWndProc = ProcessWindowMessage, // An implicit delegate is being created here and is then collected.
    hInstance = (HINSTANCE)HInstance.DangerousGetHandle(),
    hCursor = LoadCursor(default, szCursorName),
    hbrBackground = default,
    hIcon = default,
    lpszClassName = lpszClassName
};

A second best solution is to create a static field to store the Delegate.

private static WNDPROC s_WinProc;

...

s_WinProc = ProcessWindowMessage;
WNDCLASSEXW windowClass = new()
{
    cbSize = (uint)Unsafe.SizeOf<WNDCLASSEXW>(),
    style = style,
    lpfnWndProc = s_WinProc,
    hInstance = (HINSTANCE)HInstance.DangerousGetHandle(),
    hCursor = LoadCursor(default, szCursorName),
    hbrBackground = default,
    hIcon = default,
    lpszClassName = lpszClassName
};

OK, assigning ProcessWindowMessage to a static field works. So I’ll use that for now.