C# 单声道 p/调用失败
最近,我尝试在 mono(.NET for Linux 平台)中使用 p/invoke 编写一个简单的 OpenGL 应用程序,以了解它如何在 C# 上工作(我已经在 Windows 上成功完成了)。 我听说过 tao 框架,但我不想要一个简单的“hello world”之类的程序的一切。
我刚开始就卡住了。 我调用了一些 GL 函数来看看它们是否有效。 我立即调用 glClearColor 和 glClear 来查看它是否设置了 glGetError (因为此时 opengl 尚未初始化)。
它不会调用该函数,而是崩溃并转储以下堆栈跟踪和其他调试信息。 没有抛出异常。
Stacktrace:
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 是浮点型,正如我在标头中看到的声明一样。
Recently I tried to use p/invoke in mono (.NET for Linux platform) to write a simple OpenGL application to find out how it works on C# (I've already sucessfully done it on windows). I heard about the tao framework, but I don't want everything for a simple "hello world" like program.
I just stucked at the start. I p/invoked some GL functions to see if they work. I immediately called glClearColor and glClear to see whether it sets glGetError or not (due to opengl haven't been initialized at all at that point).
Instead of calling the function it just crashes and dumps the following stack trace and other debuginfo. No exception is thrown.
Stacktrace:
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>
The signature for glClearColor is:
//gllibname="opengl32.dll" --> mapped to libGL.so
[DllImport(gllibname)]
public static extern void glClearColor(float red, float green, float blue, float alpha);
C specification is:
void glClearColor( GLclampf red,GLclampf green,GLclampf blue,GLclampf alpha );
GLclampf is float as I saw its declaration in the header.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 gdb 运行该程序并准确查看 SEGV 发生的位置(请参阅 mono wiki 了解指示)。
可能的原因是代码中的其他一些不正确的 p/invoke 声明和调用损坏了内存,因此稍后会发生崩溃。
You could run the program with gdb and see exactly where the SEGV happens (see the mono wiki for instructions).
A likely cause is that some other incorrect p/invoke declaration and call in the code corrupted memory so later you get the crash.
我发现了。 我在执行 p/invoke 时使用了错误的函数签名。
I found it out. I used a wrong function signature when doing the p/invoke.