The error only occurs when compiling with -O3

When building OpenGL toolkits using GLFW and Cython, I came across a very, very strange problem. I created the following pxd file (quite large, so I used it):

https://gist.github.com/1441970

Next I have this wrapping code (extremely simplified to show the essence of the problem).

pygrafix.window module:

from pygrafix.c_headers.glfw cimport * # this is the pxd file glfwInit() _window = None cdef void _mouse_scroll_callback_handler(int pos): if _window._mouse_scroll_callback: _window._mouse_scroll_callback(_window, pos) cdef class Window: cdef public object _mouse_scroll_callback def __cinit__(self): global _window self._mouse_scroll_callback = None _window = self def __init__(self, int width = 0, int height = 0): glfwOpenWindow(width, height, 0, 0, 0, 0, 0, 0, GLFW_WINDOW) glfwSetMouseWheelCallback(<GLFWmousewheelfun> &_mouse_scroll_callback_handler) print("TEST LOCATION ONE") def is_open(self): return glfwGetWindowParam(GLFW_OPENED) def flip(self): glfwSwapBuffers() def set_mouse_scroll_callback(self, func): self._mouse_scroll_callback = func 

And this main file:

 from pygrafix import window window = window.Window(800, 600) print("TEST LOCATION TWO") def on_scroll(window, pos): print(pos) window.set_mouse_scroll_callback(on_scroll) while window.is_open(): window.flip() 

And finally, I compile it as follows:

 cython.py -o pygrafix/window.cy.c pygrafix/window.pyx gcc -O3 -shared -DGLFW_DLL -IC:\Python27\include pygrafix/window.cy.c -o pygrafix/window.pyd -LC:\Python27\libs -lpython27 -lgfwldll 

But it crashes (windows say that there was an error in the program). When I comment on the glfwSetMouseWheelCallback call, it does not crash. The strange thing is, if I compile with -O0 , it does not crash and works as it should! . I am completely puzzled by this. I checked the C code that cython prints out and it looks fine. _mouse_scroll_callback_handler is of type void (*)(int) , and a pointer to it passes well to glfw.

Other oddities:

  • This only happens with glfwSetMouseWheelCallback (or, at least for me), and not with glwSetMousePosCallback, for example.
  • If I pass NULL to glfwSetMouseWheelCallback , no problem.
  • Even if a failure occurs, TEST LOCATION ONE is still printed, but TEST LOCATION TWO does not.
  • It also works if I compile with -O3 -pg

What could be the reason for this and what would be the correct fix (without compiling with -O0)?


Other little things:

I use Windows 7 64 bit, GLFW 2.7.2, Cython 0.15.1, GCC 4.6.1 in MinGW and CPython 2.7.2.

+4
source share
1 answer

I finally found a solution. The problem was that I did not add __stdcall in Cython to the callback functions, and I also did not know that this supported.

+2
source

Source: https://habr.com/ru/post/1385066/


All Articles