I'm developing a Python module that needs to get a pointer to a NSView in order to attach OpenGL to a window.
I'm using wxPython as GUI library that has a method GetHandle() that, as the documentation says, 'Returns the platform-specific handle (as a long integer) of the physical window'.
Because my module has to be compatible with both Windows and Mac, I've made a wrapper function that takes a unsigned long and cast it to a void* in order to pass it to the actual method that handles the OpenGL creation.
void wrap_CreateContext(unsigned long windowId)
{
return CreateContext((void*)&(windowId));
}
On Windows everything works, but on Mac, when I try to cast the void* to a NSView* and then use that NSView*, the debugger gives me a EXC_BAD_ACCESS error.
This is how I cast the void* to a NSView*:
void CreateContext(void* windowId)
{
NSView* view = (NSView*)windowId;
// for example I try to get the view size
NSSize size = view.frame.size; // <--- EXC_BAD_ACCESS error
}
I really don't know if the problem is in my casting at first from unsigned long to void* and then from void* to NSView*, or if the problem is somewhere else.
The version of wxPython I'm using is build upon Cocoa so I should get a pointer to a NSView from GetHandle().
Found the solution. The problem was in my casting from long to void* and back to NSView*.
Casting directly from long to NSView* works fine.