We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 1065

Summary: osx: uncaught exception NSInternalInconsistencyException, error creating CGSWindow
Product: SDL Reporter: Marc L <rhaamo>
Component: videoAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: toomas.laasik
Version: 1.2.14   
Hardware: Other   
OS: Mac OS X 10.6   
Attachments: gdb backtrace
Fixed version of submitted file

Description Marc L 2010-10-20 13:22:52 UTC
Created attachment 540 [details]
gdb backtrace

When trying a very simple SDL test program it fails with a traceback saying:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
*** call stack here
terminate called after throwing an instance of 'NSException'

I've attached the gdb backtrace, and the test code is the following:

/*
 * Built with
 * cc -g -ggdb3 -fno-inline -o test_sdl test_sdl.c -I/opt/local/include -L/opt/local/lib -lsdl -lsdlmain
 */
void run();

int main(int argc, char *argv[]) {
    run();
}

#include <SDL/SDL.h>

void run()
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_SetVideoMode(640,480,0,SDL_OPENGL);
}
Comment 1 Toomas Laasik 2010-12-20 14:26:45 UTC
Created attachment 551 [details]
Fixed version of submitted file
Comment 2 Toomas Laasik 2010-12-21 03:11:05 UTC
I think that this is technically not a bug, but just the way how SDL works.

To fix the submitted case you have to do 2 things:
  - #include <SDL/SDL.h> before main
  - add linker flag -Wl,-framework,Cocoa
  - (it is a also good idea to exit gracefully with SDL_Quit)

SDL uses C preprocessor to redefine keyword "main". This is how it grabs control before any of your code executes and also makes program entry point cross-platform (eg windows had WinMain for graphical apps, but you still use main in SDL as entry point). The redefined main on OSX runs code that prepares native Cocoa framework for creating window (a bunch of NS... calls). If you manage to put main before you include SDL.h, then you'll get those "NS not properly initialized"-type errors.