简体   繁体   English

为什么调用委托后我的程序立即崩溃?

[英]Why does my program crash immediately after calling delegate?

I'm trying to wrap this function defined by SDL2. 我正在尝试包装SDL2定义的此函数

It's signature is 它的签名是

void SDL_AddEventWatch(SDL_EventFilter filter, void* userdata)

Where SDL_EventFilter is SDL_EventFilter在哪里

typedef int (SDLCALL * SDL_EventFilter) (void *userdata, SDL_Event * event);

Thus, I've defined my wrapper like so: 因此,我已经定义了我的包装器,如下所示:

public delegate int EventFilter(IntPtr userData, IntPtr type);

[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_AddEventWatch")]
public static extern void AddEventWatch(EventFilter filter, IntPtr userData);

And I'm testing it like so: 我正在像这样测试它:

SDL.AddEventWatch((data, e) =>
{
    return 0;
}, IntPtr.Zero);

When I run my program it actually enters the lambda function, but then immediately crashes as soon as it exits the function ("vshost32.exe has stopped working"). 当我运行程序时,它实际上进入了lambda函数,但是一旦退出该函数,便立即崩溃(“ vshost32.exe已停止工作”)。

What might be causing the crash? 是什么原因导致坠机?

 #define SDLCALL __cdecl

You have a calling convention mismatch. 您有一个呼叫约定不匹配。 Your native code requires a __cdecl function but your C# code declares a delegate that will be mapped to a an __stdcall callback. 您的本机代码需要__cdecl函数,但是您的C#代码声明了一个将映射到__stdcall回调的委托。 The default for unmanaged code interop. 非托管代码互操作的默认值。 You must declare it like this: 您必须这样声明:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int EventFilter(IntPtr userData, IntPtr type);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么调用Document.GetElementsByTagName(“ img”)[109]导致我的程序崩溃? - Why does calling Document.GetElementsByTagName(“img”)[109] cause my program to crash? 为什么我的简单C#程序立即退出? - Why does my simple C# program quit immediately? 为什么我的线程在显示Windows窗体后立即终止? - Why Does my Thread Terminate Immediately After Showing a Windows Form? C# WPF 为什么我的程序在其他计算机上崩溃? - C# WPF Why Does my program crash on other computers? 为什么我的程序会因无效的 json 原始错误而崩溃? - why does my program crash with an invalid json primitive error? 为什么抛出程序崩溃但返回却没有崩溃? - Why does throw crash my program but return doesn't? 为什么我的控制台应用程序立即消失 - Why does my console application disappear immediately 为什么我的SqlCacheDependency HasChanged返回false但几乎在更改为true后立即返回? - Why does my SqlCacheDependency HasChanged come back false but almost immediately after changes to true? 为什么我的程序不读取索引 0 之后的后续字母? - Why my program does not read subsuquent letters after index 0? 为什么异步委托方法需要调用EndInvoke? - Why does asynchronous delegate method require calling EndInvoke?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM