C#mono p / invoke失败

时间:2009-05-12 07:47:27

标签: c# mono pinvoke sigsegv

最近我尝试在mono(.NET for Linux平台)中使用p / invoke来编写一个简单的OpenGL应用程序,以了解它如何在C#上运行(我已经在Windows上成功完成了它)。我听说过tao框架,但我并不希望所有内容都像程序一样简单的“hello world”。

我刚开始时就坚持了下来。我p /调用了一些GL函数来查看它们是否有效。我立即调用glClearColor和glClear来查看它是否设置了glGetError(由于opengl在那时根本没有被初始化)。

它不是调用函数,而是崩溃并转储以下堆栈跟踪和其他debuginfo。没有例外。

堆栈跟踪:

  at (wrapper managed-to-native) Calmarius.OGL.OpenGLLibrary.glClearColor (single,single,single,single) <0x00004>
  at (wrapper managed-to-native) Calmarius.OGL.OpenGLLibrary.glClearColor (single,single,single,single) <0xffffffff>
  at Calmarius.RTS.GameForm.OnPaint (System.Windows.Forms.PaintEventArgs) [0x00000] in /home/calmarius/Development/csharp/RTS/RTS/Form1.cs:60
  at System.Windows.Forms.Control.WmPaint (System.Windows.Forms.Message&) <0x000b0>
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message&) <0x001e2>
  at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message&) <0x0000d>
  at System.Windows.Forms.ContainerControl.WndProc (System.Windows.Forms.Message&) <0x00054>
  at System.Windows.Forms.Form.WndProc (System.Windows.Forms.Message&) <0x001da>
  at ControlWindowTarget.OnMessage (System.Windows.Forms.Message&) <0x00014>
  at ControlNativeWindow.WndProc (System.Windows.Forms.Message&) <0x00022>
  at System.Windows.Forms.NativeWindow.WndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) <0x001b7>
  at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG&) <0x00016>
  at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG&) <0x00015>
  at System.Windows.Forms.Application.RunLoop (bool,System.Windows.Forms.ApplicationContext) <0x00997>
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext) <0x0006a>
  at System.Windows.Forms.Application.Run (System.Windows.Forms.Form) <0x00025>
  at Calmarius.RTS.Program.Main () [0x0000b] in /home/calmarius/Development/csharp/RTS/RTS/Program.cs:19
  at (wrapper runtime-invoke) System.Object.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff>

glClearColor的签名是:

//gllibname="opengl32.dll" --> mapped to libGL.so
[DllImport(gllibname)]

public static extern void glClearColor(float red, float green, float blue, float alpha);

C规范是:

void glClearColor( GLclampf red,GLclampf green,GLclampf blue,GLclampf alpha );

当我在标题中看到它的声明时,GLclampf是浮动的。

2 个答案:

答案 0 :(得分:3)

您可以使用gdb运行程序并查看SEGV的确切位置(有关说明,请参阅单声道wiki。)

可能的原因是某些其他错误的p / invoke声明并在代码中调用损坏的内存,以便稍后崩溃。

答案 1 :(得分:0)

我发现了。我在执行p / invoke时使用了错误的函数签名。

相关问题