.net - AccessViolationException with GLFW in C# -
i have problem glfwsetcharcallback function. whenever call it, glfwpollevents throws accessviolationexception saying: "attempted read or write protected memory. indication other memory corrupt."
i have created simple wrapper demonstrate problem:
using system; using system.runtime.interopservices; using system.security; namespace test { public delegate void glfwcharcallback(glfwwindow window, char character); [structlayout(layoutkind.explicit)] public struct glfwmonitor { private glfwmonitor(intptr ptr) { _nativeptr = ptr; } [fieldoffset(0)] private readonly intptr _nativeptr; public static readonly glfwmonitor null = new glfwmonitor(intptr.zero); } [structlayout(layoutkind.explicit)] public struct glfwwindow { private glfwwindow(intptr ptr) { _nativeptr = ptr; } [fieldoffset(0)] private readonly intptr _nativeptr; public static glfwwindow null = new glfwwindow(intptr.zero); } public class wrap { [dllimport("glfw3", callingconvention = callingconvention.cdecl), suppressunmanagedcodesecurity] private static extern int32 glfwinit(); [dllimport("glfw3", callingconvention = callingconvention.cdecl), suppressunmanagedcodesecurity] internal static extern glfwwindow glfwcreatewindow(int32 width, int32 height, [marshalas(unmanagedtype.lpstr)] string title, glfwmonitor monitor, glfwwindow share); [dllimport("glfw3", callingconvention = callingconvention.cdecl), suppressunmanagedcodesecurity] internal static extern void glfwsetcharcallback(glfwwindow window, glfwcharcallback callback); [dllimport("glfw3", callingconvention = callingconvention.cdecl), suppressunmanagedcodesecurity] internal static extern void glfwpollevents(); public static boolean init() { return glfwinit() == 1; } public static glfwwindow createwindow(int32 width, int32 height, string title, glfwmonitor monitor, glfwwindow share) { return glfwcreatewindow(width, height, title, monitor, share); } public static void setcharcallback(glfwwindow window, glfwcharcallback callback) { glfwsetcharcallback(window, callback); } public static void pollevents() { glfwpollevents(); } } }
and call glfw:
using system; namespace test { class program { static void main() { wrap.init(); var window = wrap.createwindow(800, 600, "none", glfwmonitor.null, glfwwindow.null); wrap.setcharcallback(window, (glfwwindow, character) => console.writeline(character)); while(true) { wrap.pollevents(); } } } }
the character printed console , accessviolationexception.
what doing incorrectly? dllimports specify cdecl calling convention (i've tried others on both pollevents , setcharcallback), i've tried charsets on setcharcallback function , nothing worked.
could please me?
edit:
here glfw3 dll i'm using: http://www.mediafire.com/?n4uc2bdiwdzddda
edit 2:
using newest "lib-msvc110" dll made glfwsetcharcallback work. glfwsetkeycallback , glfwsetcursorposcallback not work , continue throw accessviolationexception. attempt figure out makes charcallback special, honestly, i've gone through glfw source code through , through , functions seem thing in same way. maybe there's i'm overlooking.
edit 3:
i've tried everything, cdecl, stdcall, charsets, versions of dll, combinations of arguments, etc. i've made sure callbacks not disposed storing reference them. i've tried purposefully crash application not reserving space of arguments of glfwsetcharcallback (which works of now) , haven't managed it. leads me believe arguments have no bearing on application.
the thing makes me think fault glfw fact if compile against x86-64 dll, works perfectly. little bit strange use cdecl on x86-64, because msdn states calling convention fastcall.
it took me while, solved it. delegates describing c# functions, cannot called c. solution make delegates describe c-like functions, can achieved via unmanagedfunctionpointer
attribute.
adding attribute delegates (as shown below) solved problem.
[unmanagedfunctionpointer(callingconvention.cdecl), suppressunmanagedcodesecurity] public delegate void glfwcharcallback(glfwwindow window, char character);
Comments
Post a Comment