Programmatically creating a window in Mac OS X

Programmatically creating windows (i.e. not using a NIB file) can be tricky on the Mac. Especially if you're developing in C or C++ instead of Objective-C. Here's one of the issues that may come up.

Assuming you got the window creation code right, you may be thinking everything is going to work, but you get this error:

Sun Jan 10 20:27:54 macbook-pro.local cocoatest[55526] : kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
Sun Jan 10 20:27:54 macbook-pro.local cocoatest[55526] : kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.
Sun Jan 10 20:27:54 macbook-pro.local cocoatest[55526] : kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
Sun Jan 10 20:27:54 macbook-pro.local cocoatest[55526] : kCGErrorInvalidConnection: CGSNewWindowWithOpaqueShape: Invalid connection
2010-01-10 20:27:54.787 cocoatest[55526:a0f] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'


This means your NSApplication is not being initialized. In other words, the Objective-C side of your system needs to be properly initialized. Adding the following line may help:

[NSApplication sharedApplication];

Refer to the Apple documentation of NSApplication for more help.

Soon, I will post the complete code that creates an OpenGL Cocoa window from a C++ application without a NIB (as soon as I figure it out).

4 comments:

Anonymous said...

Hi, I met the same problem when I compile my C++ ogl code. I look forward seeing your solutions. Thanks.

Anonymous said...

Hi, I met the same problem when I compile my C++ ogl code. I look forward seeing your solutions. Thanks.

Update:
I get the same errors even when I run this code in the Xcode IDE. Do you know what's wrong?

My email is sny1985@gmail.com

Anonymous said...

just run glutInit(&argc, argv); before any initialization code in the main function, worked for me.

Eduardo said...

I needed a solution that doesn't depend on GLUT.