Important: Please read the Qt Code of Conduct -

hangs after deleteLater if aplication is run in Xfvb or with '-platform offscreen'

  • My Qt5 application has a standard main program of the form

        QApplication app{argc, argv};
        new MyMainWindow; // inherits from QMainWindow
        return app.exec();

    Termination is triggered within MyMainWindow through a call of deleteLater().

    For functional testing under Gitlab-CI, the application shall be run without opening an actual X display. I found two ways to do so:
    (1) Pass the arguments -platform offscreen to QApplication;
    (2) Xvfb :1 -screen 0 1024x768x16 &> xvfb.log & followed by DISPLAY=:1.0 and export DISPLAY.

    Everything works fine until deleteLater is called. At this point, instead of terminating, the application hangs - at difference from the correct behavior when run with regular X display. What's wrong?

  • Lifetime Qt Champion

    @Joachim-W Why do you allocate MainWindow on the heap? Allocate it on the stack and there should not be any problem.
    If you for some reason still want to allocate it on the heap then do:

    QApplication app{argc, argv};
    MyMainWindow *w =  new MyMainWindow; // inherits from QMainWindow
    auto result = app.exec();
    delete w;
    return result;

    Do not use deleteLater on MainWindow.

  • @jsulm Neither stack allocation nor an explicite delete w work for me. Somehow ~MyMainWindow is called from within app.exec(). Here the stack trace:

    #9  0x00007ffff78b9b29 in MainWin::~MainWin() (this=0x5171c0)
        at /G/sw/Steca/gui/mainwin.cpp:106
    #10 0x00007ffff6d591a0 in QObject::event(QEvent*) ()
        at /usr/lib/x86_64-linux-gnu/
    #11 0x00007ffff7aeaa1b in QWidget::event(QEvent*) ()
        at /usr/lib/x86_64-linux-gnu/
    #12 0x00007ffff7bf0de4 in QMainWindow::event(QEvent*) ()
        at /usr/lib/x86_64-linux-gnu/
    #13 0x00007ffff7aac4b1 in QApplicationPrivate::notify_helper(QObject*, QEvent*)
        () at /usr/lib/x86_64-linux-gnu/
    #14 0x00007ffff7ab3950 in QApplication::notify(QObject*, QEvent*) ()
        at /usr/lib/x86_64-linux-gnu/
    --Type <RET> for more, q to quit, c to continue without paging--c
    #15 0x00007ffff6d2f5a9 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () at /usr/lib/x86_64-linux-gnu/
    #16 0x00007ffff6d3259b in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () at /usr/lib/x86_64-linux-gnu/
    #17 0x00007ffff6d81233 in  () at /usr/lib/x86_64-linux-gnu/
    #18 0x00007ffff5636f2e in g_main_context_dispatch () at /usr/lib/x86_64-linux-gnu/
    #19 0x00007ffff56371c8 in  () at /usr/lib/x86_64-linux-gnu/
    #20 0x00007ffff563725c in g_main_context_iteration () at /usr/lib/x86_64-linux-gnu/
    #21 0x00007ffff6d80863 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () at /usr/lib/x86_64-linux-gnu/
    #22 0x00007ffff2e403e1 in  () at /usr/lib/x86_64-linux-gnu/
    #23 0x00007ffff6d2e27b in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () at /usr/lib/x86_64-linux-gnu/
    #24 0x00007ffff6d36262 in QCoreApplication::exec() () at /usr/lib/x86_64-linux-gnu/

  • calling deleteLater() an any top level window seems superfluous to me. As @jsulm said, just create it on the main() stack and trust that the framework will handle accordingly.

    If I had to venture a guess, the behaviour U R seeing is that the window system is deleting the window BEFORE the object destructor is actually called, then the destructor freaks out becuase there is no longer a valid handle to the native window.

  • @Kent-Dorfman How then to quit the application? I have a "quit" trigger connected to ptrMyMainWindow->deleteLater().

  • Lifetime Qt Champion

    Most of the time Qt will handle quitting the app automatically when last window is closed.
    Default this is true

  • Lifetime Qt Champion

  • This post is deleted!

  • Lifetime Qt Champion

    you can just call
    on it.
    If last toplevel window, the application will also quit and clean up.

Log in to reply