Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QOpenGLContext::makeCurrent, UD2 crash when resizing window on macOS 10.15



  • I'm working on a very complex program that uses OpenGL. I'm fairly new to macOS development and mainly work on Linux. There's a lot in common with the two but I'm experiencing strange issues on Mac that I don't get on Windows or Linux.

    Using a custom build of 5.14.1, in our main render loop when resizing the application window, it immediately crashes.
    This function fails:

    // _qglContext is a QOpenGLContext*
    bool result = _qglContext->makeCurrent(_window);
    

    This is where it stops in assembly:

        0x7fff300a0c31 <+513>: leaq   0x7eff99(%rip), %rdi      ; "-[NSOpenGLContext update] must be called from the main thread if the context has a view."
        0x7fff300a0c38 <+520>: callq  0x7fff308197ca            ; symbol stub for: _os_crash
    ->  0x7fff300a0c3d <+525>: ud2
    

    And here is the call stack:

       AppKit!-[NSOpenGLContext update] (Unknown Source:0)
       libqcocoa.dylib!___lldb_unnamed_symbol1148$$libqcocoa.dylib (Unknown Source:0)
       libqcocoa.dylib!___lldb_unnamed_symbol1146$$libqcocoa.dylib (Unknown Source:0)
       QtGui!QOpenGLContext::makeCurrent(QSurface*) (Unknown Source:0)
    -> libgl.dylib!gl::Context::makeCurrent() (/Users/maki/git/interface/libraries/gl/src/gl/ContextQt.cpp:89)
       libdisplay-plugins.dylib!PresentThread::run() (/Users/maki/git/interface/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp:231)
       QtCore!___lldb_unnamed_symbol236$$QtCore (Unknown Source:0)
       libsystem_pthread.dylib!_pthread_start (Unknown Source:0)
       libsystem_pthread.dylib!thread_start (Unknown Source:0)
    

    As far as I can tell, this is not Qt's problem but macOS's OpenGL library?

    I'm looking everywhere through out our code base and I just can't find what's going on. The assembly comments tells it must be called from the main thread which I think it is. There's no mutex locks or moving threads that immediately happen when resizing. I've commented out every bit of code that handles window resizing and it still persists.

    It's been 2 whole days of debugging and I'm not sure what to do anymore. I've looked around online and found:
    https://forum.qt.io/topic/107879/qt3d-examples-crashing-on-mac-os-catalina
    https://bugreports.qt.io/browse/QTBUG-79315

    The bug report says it's fixed in 5.14 which I'm using. It might not be the same problem however because there it happens in NSOpenGLContext setView where as for me it's in NSOpenGLContext update

    I'm tackling a huge code base and I may be missing something but I thought I would ask here for opinions.
    Thank you, Maki



  • Inside of qopenglcontext.cpp there is:

    bool QOpenGLContext::makeCurrent(QSurface *surface)
    {
        Q_D(QOpenGLContext);
        if (!isValid())
            return false;
    
        if (Q_UNLIKELY(!qApp->testAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity)
                       && thread() != QThread::currentThread())) {
            qFatal("Cannot make QOpenGLContext current in a different thread");
        }
    
        if (!surface) {
            doneCurrent();
            return true;
        }
    
        if (!surface->surfaceHandle())
            return false;
        if (!surface->supportsOpenGL()) {
            qWarning() << "QOpenGLContext::makeCurrent() called with non-opengl surface" << surface;
            return false;
        }
    
        if (!d->platformGLContext->makeCurrent(surface->surfaceHandle()))
            return false;
    
        QOpenGLContextPrivate::setCurrentContext(this);
    #ifndef QT_NO_DEBUG
        QOpenGLContextPrivate::toggleMakeCurrentTracker(this, true);
    #endif
    

    At the start, it compares thread() to QThread::currentThread() which isn't failing in my program.


    -[NSOpenGLContext update] must be called from the main thread if the context has a view.

    If the context has a view. surfaceHandle?

    I'll try to figure out which line is causing the crash to narrow it down.



  • I was able to fix it by using using Xcode SDK version 10.13 from here:
    https://github.com/phracker/MacOSX-SDKs

    This still doesn't solve the problem but maybe the next macOS will fix it. In the past, my program used to crash on Catalina but suddenly started working again. I heard OpenGL may be deprecated now which is terrible...



  • @Makitsune My app crashes with the same error after linking a ggc static lib to a llvm project. And even I modified my cmake-based project in QtCreator the wrong setting was still used. I deleted the kit and set it up again to fix the error.


Log in to reply