来自大型数组的P/Invoke调用上的c#System.executionEngineer错误

来自大型数组的P/Invoke调用上的c#System.executionEngineer错误,c#,winapi,pinvoke,C#,Winapi,Pinvoke,我正在用c编写一个小应用程序,使用p/Invoke创建一个窗口。我得到一个System.executionEngineeException在处理大型数组时由PeekMessage引发。这很奇怪,因为当实际使用数组时,不会抛出异常,而且一切都很好。但是当我调用peek消息时,它会抛出。不创建阵列可以防止问题的发生。发生异常时,会将其打印到控制台: Process terminated. A callback was made on a garbage collected delegate of t

我正在用c编写一个小应用程序,使用p/Invoke创建一个窗口。我得到一个
System.executionEngineeException
在处理大型数组时由
PeekMessage
引发。这很奇怪,因为当实际使用数组时,不会抛出异常,而且一切都很好。但是当我调用
peek消息时,它会抛出。不创建阵列可以防止问题的发生。发生异常时,会将其打印到控制台:

Process terminated. A callback was made on a garbage collected delegate of type 'PInvoke.User32!PInvoke.User32+WndProc::Invoke'.
   at PInvoke.User32.PeekMessage(MSG*, IntPtr, WindowMessage, WindowMessage, PeekMessageRemoveFlags)
   at PInvoke.User32.PeekMessage(MSG*, IntPtr, WindowMessage, WindowMessage, PeekMessageRemoveFlags)
   at PInvoke.User32.PeekMessage(IntPtr, IntPtr, WindowMessage, WindowMessage, PeekMessageRemoveFlags)
   at Program.Main(System.String[])
我能够从我的项目中拼凑出一个小样本来重现错误。我正在使用.NETCore3.1

using System;
using System.Drawing;
using static PInvoke.Kernel32;
using static PInvoke.User32;

static class Program
{
    static IntPtr HWND;

    static unsafe void Main(string[] args)
    {
        // creating the arrays on lines 14, 16, and 26 somehow cause the error and creating the window after line 18 doesn't cause the error.

        HWND = CreateWindow();

        byte[] b = new byte[100000000];

        DoSomething(new int[10000]);

        PeekMessage(HWND, HWND, 0, 0, PeekMessageRemoveFlags.PM_REMOVE);
    }

    public static void DoSomething(int[] data)
    {
        Color[] result = new Color[data.Length];
    }

    static unsafe IntPtr WndProc(IntPtr hwnd, WindowMessage msg, void* wParam, void* lParam)
    {
        return DefWindowProc(hwnd, msg, (IntPtr)wParam, (IntPtr)lParam);
    }

    static unsafe IntPtr CreateWindow()
    {
        var hIsnt = GetModuleHandle(null);

        string classname = "test";
        WNDCLASS wndclass;

        fixed (char* pClassName = classname)
            wndclass = new WNDCLASS()
            {
                lpfnWndProc = WndProc,
                hInstance = hIsnt.DangerousGetHandle(),
                lpszClassName = pClassName
            };

        RegisterClass(ref wndclass);

        var hwnd = PInvoke.User32.CreateWindow(
            classname,
            "test",
            WindowStyles.WS_CAPTION |
            WindowStyles.WS_VISIBLE,
            100, 100, 1280, 720,
            IntPtr.Zero, IntPtr.Zero,
            hIsnt.DangerousGetHandle(),
            IntPtr.Zero
            );

        return hwnd;
    }

    static unsafe void HandleEvents()
    {
        MSG msg;
        while (PeekMessage(&msg, HWND, 0, 0, PeekMessageRemoveFlags.PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
}

我很感激你能提供的任何帮助

我明白了!问题是,传递给非托管代码的WndProc委托被GC释放,使用大型数组会导致垃圾收集。简单的修复方法是保留WndProc委托传递到window类中的引用。

要从NativeWindow派生自己的类,最好调用其AssignHandle()方法。该类确保在关闭窗口之前不能对对象进行垃圾收集。