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 2982

Summary: SDL_CreateWindowFrom() crash with Qt widget under Mac OS
Product: SDL Reporter: François Colas <francois>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED WONTFIX QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.0   
Hardware: x86_64   
OS: Mac OS X (All)   

Description François Colas 2015-05-12 09:41:09 UTC
I am using libSDL2 in a Qt app. The code is working well under Linux and Windows but is failing under Mac OS. Here is the crash output:

2015-05-12 10:24:35.598 testapp[4621:105425] -[QNSView title]: unrecognized selector sent to instance 0x7fdfbac7b8e0
2015-05-12 10:24:35.643 testapp[4621:105425] An uncaught exception was raised
2015-05-12 10:24:35.643 testapp[4621:105425] -[QNSView title]: unrecognized selector sent to instance 0x7fdfbac7b8e0
2015-05-12 10:24:35.643 testapp[4621:105425] (
    0   CoreFoundation                      0x00007fff9332764c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff967686de objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff9332a6bd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00007fff93271a84 ___forwarding___ + 1028
    4   CoreFoundation                      0x00007fff932715f8 _CF_forwarding_prep_0 + 120
    5   testapp                             0x000000010446af59 Cocoa_CreateWindowFrom + 73
    6   testapp                             0x000000010445fe1d SDL_CreateWindowFrom_REAL + 205
    7   testapp                             0x000000010417c83c main + 380
    8   testapp                             0x000000010417c6b4 start + 52
    9   ???                                 0x0000000000000001 0x0 + 1
)

That sample code reproduce the crash. Again it is working under Linux and Windows but not under Mac OS.

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    if (SDL_VideoInit(NULL) != 0) {
        qCritical("SDL_VideoInit() error: %s", SDL_GetError());
        return EXIT_FAILURE;
    }

    QWidget*    qwidget   = new QWidget();
    SDL_Window* sdlWindow = SDL_CreateWindowFrom((void*) qwidget->winId());
    if (sdlWindow == NULL) {
        qCritical("SDL_CreateWindowFrom error: %s", SDL_GetError());
        return EXIT_FAILURE;
    }

    return app.exec();
}

According to Qt's documentation, QWidget::winId() return a pointer to a NSView. Looking at SDL's source code it seems that Cocoa_CreateWindowFrom() function takes a NSWindow.

Does it means that we can't embed a SDL widget in a app window? Do you think Cocoa_CreateWindowFrom() could be updated to take a NSView?
Comment 1 Sam Lantinga 2016-10-08 00:41:34 UTC
That's correct, although there may be some way to get an NSWindow from an NSView, I don't know.