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

QWidget/QDialog set Qt::WA_DeleteOnClose will crash the application when close the main window



  • The code is very simple, just create a QPushButton and a QDialog.
    mymac:> qmake -v
    QMake version 3.1
    Using Qt version 5.11.2 in /usr/local/Cellar/qt/5.11.2_1/lib

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void
    MainWindow::on_pushButton_clicked()
    {
    MyDialog* dialog = new MyDialog(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->show();
    }

    mydialog.cpp:

    MyDialog::MyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyDialog)
    {
    ui->setupUi(this);
    }

    MyDialog::~MyDialog()
    {
    delete ui;
    }

    when running on Mac,
    mymac:> ./dialogtest.app/Contents/MacOS/dialogtest
    dialogtest(72739,0x7fffb02bf380) malloc: *** error for object 0x7ffee71ce978: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    Abort trap: 6 (core dumped)

    crash report generated by MacOs:

    Process: dialogtest [72739]
    Path: /Users/USER/*/dialogtest.app/Contents/MacOS/dialogtest
    Identifier: com.yourcompany.dialogtest
    Version: 0
    Code Type: X86-64 (Native)
    Parent Process: bash [86735]
    Responsible: dialogtest [72739]
    User ID: 501

    Date/Time: 2019-01-25 23:19:02.812 -0800
    OS Version: Mac OS X 10.13.6 (17G3025)
    Report Version: 12
    Anonymous UUID: 96A1AF96-6E32-4D77-67FA-CA4895814B40

    Sleep/Wake UUID: 77FB1050-2557-4CAB-BFD8-29E3D5CDC929

    Time Awake Since Boot: 420000 seconds
    Time Since Wake: 5100 seconds

    System Integrity Protection: disabled

    Crashed Thread: 0 Dispatch queue: com.apple.main-thread

    Exception Type: EXC_CRASH (SIGABRT)
    Exception Codes: 0x0000000000000000, 0x0000000000000000
    Exception Note: EXC_CORPSE_NOTIFY

    Application Specific Information:
    abort() called
    *** error for object 0x7ffee71ce978: pointer being freed was not allocated

    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0 libsystem_kernel.dylib 0x00007fff779c9b66 __pthread_kill + 10
    1 libsystem_pthread.dylib 0x00007fff77b94080 pthread_kill + 333
    2 libsystem_c.dylib 0x00007fff779251ae abort + 127
    3 libsystem_malloc.dylib 0x00007fff77a23822 free + 521
    4 org.qt-project.QtCore 0x00000001097172c2 QObject::event(QEvent*) + 146
    5 org.qt-project.QtWidgets 0x0000000108a8c798 QWidget::event(QEvent*) + 4776
    6 org.qt-project.QtWidgets 0x0000000108b9cfa5 QMainWindow::event(QEvent*) + 277
    7 org.qt-project.QtWidgets 0x0000000108a51a7d QApplicationPrivate::notify_helper(QObject*, QEvent*) + 269
    8 org.qt-project.QtWidgets 0x0000000108a52e0e QApplication::notify(QObject*, QEvent*) + 574
    9 org.qt-project.QtCore 0x00000001096ee4b4 QCoreApplication::notifyInternal2(QObject*, QEvent*) + 212
    10 org.qt-project.QtCore 0x00000001096ef6ae QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) + 878
    11 libqcocoa.dylib 0x000000010bdb44f9 0x10bd88000 + 181497
    12 libqcocoa.dylib 0x000000010bdb4d70 0x10bd88000 + 183664
    13 com.apple.CoreFoundation 0x00007fff4f9658f1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
    14 com.apple.CoreFoundation 0x00007fff4fa1f30c __CFRunLoopDoSource0 + 108
    15 com.apple.CoreFoundation 0x00007fff4f948350 __CFRunLoopDoSources0 + 208
    16 com.apple.CoreFoundation 0x00007fff4f9477cd __CFRunLoopRun + 1293
    17 com.apple.CoreFoundation 0x00007fff4f947033 CFRunLoopRunSpecific + 483
    18 com.apple.HIToolbox 0x00007fff4ec31d96 RunCurrentEventLoopInMode + 286
    19 com.apple.HIToolbox 0x00007fff4ec31a0f ReceiveNextEventCommon + 366
    20 com.apple.HIToolbox 0x00007fff4ec31884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
    21 com.apple.AppKit 0x00007fff4cee1a73 _DPSNextEvent + 2085
    22 com.apple.AppKit 0x00007fff4d677e34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
    23 com.apple.AppKit 0x00007fff4ced6885 -[NSApplication run] + 764
    24 libqcocoa.dylib 0x000000010bdb3c17 0x10bd88000 + 179223
    25 org.qt-project.QtCore 0x00000001096e9b0f QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) + 431
    26 org.qt-project.QtCore 0x00000001096eeac2 QCoreApplication::exec() + 130
    27 com.yourcompany.dialogtest 0x0000000108a34b4d main + 61 (main.cpp:10)
    28 libdyld.dylib 0x00007fff77879015 start + 1

    Thread 1:
    0 libsystem_pthread.dylib 0x00007fff77b90bdc start_wqthread + 0
    1 ??? 0x00000018000c0000 0 + 103080001536

    Thread 2:
    0 libsystem_kernel.dylib 0x00007fff779ca28a __workq_kernreturn + 10
    1 libsystem_pthread.dylib 0x00007fff77b9120e _pthread_wqthread + 1552
    2 libsystem_pthread.dylib 0x00007fff77b90be9 start_wqthread + 13

    Thread 3:: Dispatch queue: NSCGSDisableUpdates
    0 libsystem_kernel.dylib 0x00007fff779c020a mach_msg_trap + 10
    1 libsystem_kernel.dylib 0x00007fff779bf724 mach_msg + 60
    2 com.apple.SkyLight 0x00007fff7169d6a5 CGSUpdateManager::enable_updates_common() + 565
    3 com.apple.SkyLight 0x00007fff71641828 CGSUpdateManager::enable_update(unsigned long long) + 320
    4 libdispatch.dylib 0x00007fff778475fa _dispatch_call_block_and_release + 12
    5 libdispatch.dylib 0x00007fff7783fdb8 _dispatch_client_callout + 8
    6 libdispatch.dylib 0x00007fff77854217 _dispatch_queue_serial_drain + 635
    7 libdispatch.dylib 0x00007fff77847166 _dispatch_queue_invoke + 373
    8 libdispatch.dylib 0x00007fff77854f0d _dispatch_root_queue_drain_deferred_wlh + 332
    9 libdispatch.dylib 0x00007fff77858d21 _dispatch_workloop_worker_thread + 880
    10 libsystem_pthread.dylib 0x00007fff77b90fd2 _pthread_wqthread + 980
    11 libsystem_pthread.dylib 0x00007fff77b90be9 start_wqthread + 13

    Thread 4:
    0 libsystem_kernel.dylib 0x00007fff779ca28a __workq_kernreturn + 10
    1 libsystem_pthread.dylib 0x00007fff77b9120e _pthread_wqthread + 1552
    2 libsystem_pthread.dylib 0x00007fff77b90be9 start_wqthread + 13

    Thread 5:
    0 libsystem_kernel.dylib 0x00007fff779ca28a __workq_kernreturn + 10
    1 libsystem_pthread.dylib 0x00007fff77b9120e _pthread_wqthread + 1552
    2 libsystem_pthread.dylib 0x00007fff77b90be9 start_wqthread + 13

    Thread 6:: com.apple.NSEventThread
    0 libsystem_kernel.dylib 0x00007fff779c020a mach_msg_trap + 10
    1 libsystem_kernel.dylib 0x00007fff779bf724 mach_msg + 60
    2 com.apple.CoreFoundation 0x00007fff4f948665 __CFRunLoopServiceMachPort + 341
    3 com.apple.CoreFoundation 0x00007fff4f9479b7 __CFRunLoopRun + 1783
    4 com.apple.CoreFoundation 0x00007fff4f947033 CFRunLoopRunSpecific + 483
    5 com.apple.AppKit 0x00007fff4d01efc4 _NSEventThread + 184
    6 libsystem_pthread.dylib 0x00007fff77b91661 _pthread_body + 340
    7 libsystem_pthread.dylib 0x00007fff77b9150d _pthread_start + 377
    8 libsystem_pthread.dylib 0x00007fff77b90bf9 thread_start + 13

    Thread 0 crashed with X86 Thread State (64-bit):
    rax: 0x0000000000000000 rbx: 0x00007fffb02bf380 rcx: 0x00007ffee71ccac8 rdx: 0x0000000000000000
    rdi: 0x0000000000000307 rsi: 0x0000000000000006 rbp: 0x00007ffee71ccb00 rsp: 0x00007ffee71ccac8
    r8: 0x0000000000000000 r9: 0x0000000000000000 r10: 0x0000000000000000 r11: 0x0000000000000206
    r12: 0x0000000000000307 r13: 0x0000000000000004 r14: 0x0000000000000006 r15: 0x000000000000002d
    rip: 0x00007fff779c9b66 rfl: 0x0000000000000206 cr2: 0x00007fffb029c168

    The crash also occurred on Linux/Ubuntu OS.


  • Lifetime Qt Champion

    @ThreeStones

    MyDialog* dialog = new MyDialog(this);

    With this you parent the dialog to your window, which will likely give a you double delete.



  • @aha_1980 Thanks for your reply.

    I just tried to remove "this", but it still crash at the same point. I got same backtrace call stack.

    void
    MainWindow::on_pushButton_clicked()
    {
      MyDialog* dialog = new MyDialog();
      dialog->setAttribute(Qt::WA_DeleteOnClose,true);
      dialog->show();
    }
    

    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0 libsystem_kernel.dylib 0x00007fff779c9b66 __pthread_kill + 10
    1 libsystem_pthread.dylib 0x00007fff77b94080 pthread_kill + 333
    2 libsystem_c.dylib 0x00007fff779251ae abort + 127
    3 libsystem_malloc.dylib 0x00007fff77a23822 free + 521
    4 org.qt-project.QtCore 0x000000010db622c2 QObject::event(QEvent*) + 146
    5 org.qt-project.QtWidgets 0x000000010ced3798 QWidget::event(QEvent*) + 4776
    6 org.qt-project.QtWidgets 0x000000010cfe3fa5 QMainWindow::event(QEvent*) + 277
    7 org.qt-project.QtWidgets 0x000000010ce98a7d QApplicationPrivate::notify_helper(QObject*, QEvent*) + 269
    8 org.qt-project.QtWidgets 0x000000010ce99e0e QApplication::notify(QObject*, QEvent*) + 574
    9 org.qt-project.QtCore 0x000000010db394b4 QCoreApplication::notifyInternal2(QObject*, QEvent*) + 212
    10 org.qt-project.QtCore 0x000000010db3a6ae QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) + 878
    11 libqcocoa.dylib 0x00000001101b44f9 0x110188000 + 181497
    12 libqcocoa.dylib 0x00000001101b4d70 0x110188000 + 183664
    13 com.apple.CoreFoundation 0x00007fff4f9658f1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
    14 com.apple.CoreFoundation 0x00007fff4fa1f30c __CFRunLoopDoSource0 + 108
    15 com.apple.CoreFoundation 0x00007fff4f948350 __CFRunLoopDoSources0 + 208
    16 com.apple.CoreFoundation 0x00007fff4f9477cd __CFRunLoopRun + 1293
    17 com.apple.CoreFoundation 0x00007fff4f947033 CFRunLoopRunSpecific + 483
    18 com.apple.HIToolbox 0x00007fff4ec31d96 RunCurrentEventLoopInMode + 286
    19 com.apple.HIToolbox 0x00007fff4ec31a0f ReceiveNextEventCommon + 366
    20 com.apple.HIToolbox 0x00007fff4ec31884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
    21 com.apple.AppKit 0x00007fff4cee1a73 _DPSNextEvent + 2085
    22 com.apple.AppKit 0x00007fff4d677e34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
    23 com.apple.AppKit 0x00007fff4ced6885 -[NSApplication run] + 764
    24 libqcocoa.dylib 0x00000001101b3c17 0x110188000 + 179223
    25 org.qt-project.QtCore 0x000000010db34b0f QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) + 431
    26 org.qt-project.QtCore 0x000000010db39ac2 QCoreApplication::exec() + 130
    27 com.yourcompany.dialogtest 0x000000010ce7ab4d main + 61 (main.cpp:10)
    28 libdyld.dylib 0x00007fff77879015 start + 1


  • Lifetime Qt Champion

    @ThreeStones you are setting deleteOnClose on your main window, is that correct?

    Are you closing the dialog or the main window?



  • @aha_1980 Sorry I just correct the code. I am trying to delete the Dialog.

    I am doing similar in a complicated application and observed same crash, so I just create a simple demo app.
    If I change the code using connection signal/slot, it will not crash the application

    LutDialog *lut = new  LutDialog(this);
    //lut->setAttribute(Qt::WA_DeleteOnClose, true);
    connect(lut, &LutDialog::destroyed, lut, &LutDialog::deleteLater);
    

    This is the crash report for my real application.

    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0 org.qt-project.QtCore 0x0000000107298a58 QMetaObject::cast(QObject*) const + 24
    1 org.qt-project.QtWidgets 0x000000010660ff4b QWidget::isActiveWindow() const + 283
    2 org.qt-project.QtWidgets 0x000000010662039d QWidget::initPainter(QPainter*) const + 45
    3 org.qt-project.QtGui 0x0000000106de00ed QPainter::begin(QPaintDevice*) + 781
    4 org.qt-project.QtGui 0x0000000106ddfdd2 QPainter::QPainter(QPaintDevice*) + 242
    5 org.qt-project.QtWidgets 0x0000000106613147 QWidgetPrivate::drawWidget(QPaintDevice*, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*) + 2119
    6 org.qt-project.QtWidgets 0x00000001065edcbb 0x1065d0000 + 122043
    7 org.qt-project.QtWidgets 0x000000010661ab6a QWidget::event(QEvent*) + 1658
    8 org.qt-project.QtWidgets 0x000000010672bfa5 QMainWindow::event(QEvent*) + 277
    9 org.qt-project.QtWidgets 0x00000001065e0a7d QApplicationPrivate::notify_helper(QObject*, QEvent*) + 269
    10 org.qt-project.QtWidgets 0x00000001065e1e0e QApplication::notify(QObject*, QEvent*) + 574
    11 org.qt-project.QtCore 0x00000001072924b4 QCoreApplication::notifyInternal2(QObject*, QEvent*) + 212
    12 org.qt-project.QtCore 0x00000001072936ae QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*)



  • Found out what's wrong with my real application.

    It is crashed due to this line of code in my main.cpp

    QCoreApplication::setAttribute(Qt::AA_NativeWindows, true);
    

    I dont know why this will crash the application :(. After removal of this code, tested many times in difference scenarios, the application worked very well.


Log in to reply