Skip to content

WxQtDeleteLaterNotes

Mariano Reingart edited this page Aug 2, 2014 · 5 revisions

Using deleteLater() should be safer to avoid crashing due pending events, see [4384f4a] (https://github.com/reingart/wxWidgets/commit/4384f4a0903e3fea4fc729009d033f01fe3a33dc), 1606680 (removing superflous QPointer that was automatic calling delete) and issue #34 (memory leaks). See comments in #43 for more information.

The current code, without deleteLater (that is, using delete directly), crash:

reingart@s5ultra:~/src/wxWidgets/bldqt5/samples/controls$ gdb ./controls 
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
Leyendo símbolos desde ./controls...hecho.
(gdb) r
Starting program: /home/reingart/src/wxWidgets/bldqt5/samples/controls/controls
Program received signal SIGSEGV, Segmentation fault.
QObject::~QObject (this=0x91c600, __in_chrg=<optimised out>)at kernel/qobject.cpp:789
789	kernel/qobject.cpp: No existe el archivo o el directorio.
(gdb) bt
#0  QObject::~QObject (this=0x91c600, __in_chrg=<optimised out>) at kernel/qobject.cpp:789
#1  0x00007ffff7883f35 in wxQtShortcutHandler::~wxQtShortcutHandler (this=0x91c600, __in_chrg=<optimised out>) at ../src/qt/window.cpp:59
#2  0x00007ffff7883f64 in wxQtShortcutHandler::~wxQtShortcutHandler (this=0x91c600, __in_chrg=<optimised out>) at ../src/qt/window.cpp:59
#3  0x00007ffff7830b6c in wxQtTabWidget::currentChanged (this=0x8f00d0, index=0) at ../src/qt/notebook.cpp:34
#4  0x00007ffff7832ede in QtPrivate::FunctionPointer<void (wxQtTabWidget::*)(int)>::call<QtPrivate::List<int, void>, void> (f=(void (wxQtTabWidget::*)(wxQtTabWidget * const, int)) 0x7ffff7830b1c <wxQtTabWidget::currentChanged(int)>, o=0x8f00d0, arg=0x7fffffffc470) at /usr/include/qt5/QtCore/qobjectdefs_impl.h:153
#5  0x00007ffff7832d4b in QtPrivate::QSlotObject<void (wxQtTabWidget::*)(int), QtPrivate::List<int, void>, void>::impl (which=1, this_=0x967830, r=0x8f00d0, a=0x7fffffffc470, ret=0x0) at /usr/include/qt5/QtCore/qobject_impl.h:149
#6  0x00007ffff5386d73 in call (a=0x7fffffffc470, r=0x8f00d0, this=0x967830)at ../../include/QtCore/../../src/corelib/kernel/qobject_impl.h:132
#7  QMetaObject::activate (sender=0x8f00d0, signalOffset=<optimised out>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc470) at kernel/qobject.cpp:3561
#8  0x00007ffff53877d7 in QMetaObject::activate (sender=<optimised out>, m=m@entry=0x7ffff61cd500 <QTabWidget::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc470) at kernel/qobject.cpp:3444
#9  0x00007ffff5c9ddbe in QTabWidget::currentChanged (this=<optimised out>, _t1=0) at .moc/moc_qtabwidget.cpp:303
#10 0x00007ffff5c9e0cd in QTabWidgetPrivate::_q_showTab (this=<optimised out>, index=<optimised out>) at widgets/qtabwidget.cpp:749
#11 0x00007ffff5c9fe6f in QTabWidget::qt_static_metacall (_o=<optimised out>, _c=<optimised out>, _id=<optimised out>, _a=<optimised out>)at .moc/moc_qtabwidget.cpp:169
#12 0x00007ffff53872a6 in QMetaObject::activate (sender=sender@entry=0x8f9280, signalOffset=<optimised out>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc620) at kernel/qobject.cpp:3569
#13 0x00007ffff53877d7 in QMetaObject::activate (sender=sender@entry=0x8f9280, m=m@entry=0x7ffff61cd220 <QTabBar::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc620) at kernel/qobject.cpp:3444
#14 0x00007ffff5c906ae in QTabBar::currentChanged (this=this@entry=0x8f9280, _t1=_t1@entry=0) at .moc/moc_qtabbar.cpp:308
#15 0x00007ffff5c95d98 in QTabBar::setCurrentIndex (this=this@entry=0x8f9280, index=index@entry=0) at widgets/qtabbar.cpp:1215
#16 0x00007ffff5c9953b in QTabBar::removeTab (this=0x8f9280, index=0)at widgets/qtabbar.cpp:917
#17 0x00007ffff5c9eb08 in QTabWidgetPrivate::_q_removeTab (this=<optimised out>, index=0) at widgets/qtabwidget.cpp:755
#18 0x00007ffff5c9fe57 in QTabWidget::qt_static_metacall (_o=<optimised out>, _c=<optimised out>, _id=<optimised out>, _a=<optimised out>)at .moc/moc_qtabwidget.cpp:170
#19 0x00007ffff53872a6 in QMetaObject::activate (sender=0x8f3b10, signalOffset=<optimised out>, local_signal_index=local_signal_index@entry=1, argv=argv@entry=0x7fffffffc8e0) at kernel/qobject.cpp:3569
#20 0x00007ffff53877d7 in QMetaObject::activate (sender=<optimised out>, m=m@entry=0x7ffff61e4920 <QStackedWidget::staticMetaObject>, local_signal_index=local_signal_index@entry=1, argv=argv@entry=0x7fffffffc8e0) at kernel/qobject.cpp:3444
#21 0x00007ffff5e73b81 in QStackedWidget::widgetRemoved (this=<optimised out>, _t1=0) at .moc/moc_qstackedwidget.cpp:210
#22 0x00007ffff5e73c4b in QStackedWidget::qt_static_metacall (_o=<optimised out>, _c=<optimised out>, _id=<optimised out>, _a=<optimised out>) at .moc/moc_qstackedwidget.cpp:97
#23 0x00007ffff53872a6 in QMetaObject::activate (sender=sender@entry=0x96f950, signalOffset=<optimised out>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffca90) at kernel/qobject.cpp:3569
#24 0x00007ffff53877d7 in QMetaObject::activate (sender=sender@entry=0x96f950, m=m@entry=0x7ffff61dede0 <QStackedLayout::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffca90) at kernel/qobject.cpp:3444
#25 0x00007ffff5e6da9e in QStackedLayout::widgetRemoved (this=this@entry=0x96f950, _t1=_t1@entry=0)at .moc/moc_qstackedlayout.cpp:204
#26 0x00007ffff5b26123 in QStackedLayout::takeAt (this=0x96f950, index=0)at kernel/qstackedlayout.cpp:291
#27 0x00007ffff5b1f477 in removeWidgetRecursively (li=<optimised out>, w=0x9699b0) at kernel/qlayout.cpp:568
#28 0x00007ffff5b219a0 in QLayout::widgetEvent (this=<optimised out>, e=e@entry=0x7fffffffcd50) at kernel/qlayout.cpp:626
#29 0x00007ffff5b01c5e in QApplicationPrivate::notify_helper (this=this@entry=0x68c760, receiver=receiver@entry=0x8f3b10, e=e@entry=0x7fffffffcd50) at kernel/qapplication.cpp:3477
#30 0x00007ffff5b06e56 in QApplication::notify (this=0x68c6f0, receiver=0x8f3b10, e=0x7fffffffcd50) at kernel/qapplication.cpp:3236
#31 0x00007ffff535fc2d in QCoreApplication::notifyInternal (this=0x68c6f0, receiver=0x8f3b10, event=event@entry=0x7fffffffcd50)at kernel/qcoreapplication.cpp:881
#32 0x00007ffff538e0ae in sendEvent (event=0x7fffffffcd50, receiver=<optimised out>)at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:232
#33 QObjectPrivate::setParent_helper (this=this@entry=0x96a130, o=o@entry=0x0)at kernel/qobject.cpp:1867
#34 0x00007ffff538e652 in QObject::~QObject (this=<optimised out>, __in_chrg=<optimised out>) at kernel/qobject.cpp:939
#35 0x00007ffff5b39c7a in QWidget::~QWidget (this=0x9699b0, __in_chrg=<optimised out>) at kernel/qwidget.cpp:1354
#36 0x00007ffff78825cc in wxQtEventSignalHandler<QWidget, wxWindow>::~wxQtEventSignalHandler (this=0x9699b0, __in_chrg=<optimised out>)at ../include/wx/qt/private/winevent.h:47
#37 0x00007ffff7884046 in wxQtWidget::~wxQtWidget (this=0x9699b0, __in_chrg=<optimised out>) at ../src/qt/window.cpp:33
#38 0x00007ffff788407e in wxQtWidget::~wxQtWidget (this=0x9699b0, __in_chrg=<optimised out>) at ../src/qt/window.cpp:33
#39 0x00007ffff787cf51 in wxWindow::~wxWindow (this=0x9687e0, __in_chrg=<optimised out>) at ../src/qt/window.cpp:189
#40 0x0000000000429153 in wxNavigationEnabled<wxWindow>::~wxNavigationEnabled (
   this=0x9687e0, __in_chrg=<optimised out>)at ../../../include/wx/containr.h:182
#41 0x0000000000429208 in wxPanelBase::~wxPanelBase (this=0x9687e0, __in_chrg=<optimised out>) at ../../../include/wx/panel.h:30
#42 0x000000000042a4aa in wxPanel::~wxPanel (this=0x9687e0, __in_chrg=<optimised out>) at ../../../include/wx/generic/panelg.h:16
#43 0x000000000042a4da in wxPanel::~wxPanel (this=0x9687e0, __in_chrg=<optimised out>) at ../../../include/wx/generic/panelg.h:16
#44 0x00007ffff79991ff in wxWindowBase::Destroy (this=0x9687e0)at ../src/common/wincmn.cpp:576
#45 0x00007ffff799936a in wxWindowBase::DestroyChildren (this=0x91c600)at ../src/common/wincmn.cpp:608
#46 0x00007ffff787ccdb in wxWindow::~wxWindow (this=0x91c600, __in_chrg=<optimised out>) at ../src/qt/window.cpp:170
#47 0x00007ffff78b4869 in wxControlBase::~wxControlBase (this=0x91c600, __in_chrg=<optimised out>) at ../src/common/ctrlcmn.cpp:48
#48 0x0000000000429b26 in wxControl::~wxControl (this=0x91c600, __in_chrg=<optimised out>) at ../../../include/wx/qt/control.h:11
#49 0x00007ffff7831ee8 in wxBookCtrlBase::~wxBookCtrlBase (this=0x91c600, __in_chrg=<optimised out>) at ../include/wx/bookctrl.h:57
#50 0x00007ffff783214b in wxNotebookBase::~wxNotebookBase (this=0x91c600, __in_chrg=<optimised out>) at ../include/wx/notebook.h:104
#51 0x00007ffff7832fda in wxNotebook::~wxNotebook (this=0x91c600, __in_chrg=<optimised out>) at ../include/wx/qt/notebook.h:13
#52 0x00007ffff7833036 in wxNotebook::~wxNotebook (this=0x91c600, __in_chrg=<optimised out>) at ../include/wx/qt/notebook.h:13
#53 0x00007ffff79991ff in wxWindowBase::Destroy (this=0x91c600)at ../src/common/wincmn.cpp:576
#54 0x00007ffff799936a in wxWindowBase::DestroyChildren (this=0x8f5ba0)at ../src/common/wincmn.cpp:608
#55 0x00007ffff787ccdb in wxWindow::~wxWindow (this=0x8f5ba0, __in_chrg=<optimised out>) at ../src/qt/window.cpp:170
#56 0x0000000000429153 in wxNavigationEnabled<wxWindow>::~wxNavigationEnabled (this=0x8f5ba0, __in_chrg=<optimised out>)at ../../../include/wx/containr.h:182
#57 0x0000000000429208 in wxPanelBase::~wxPanelBase (this=0x8f5ba0, 
#58 0x000000000042a4aa in wxPanel::~wxPanel (this=0x8f5ba0, __in_chrg=<optimised out>) at ../../../include/wx/generic/panelg.h:16
#59 0x0000000000420179 in MyPanel::~MyPanel (this=0x8f5ba0, __in_chrg=<optimised out>) at ../../../samples/controls/controls.cpp:1862
#60 0x00000000004201ca in MyPanel::~MyPanel (this=0x8f5ba0, __in_chrg=<optimised out>) at ../../../samples/controls/controls.cpp:1868
#61 0x00007ffff79991ff in wxWindowBase::Destroy (this=0x8f5ba0)at ../src/common/wincmn.cpp:576
#62 0x00007ffff799936a in wxWindowBase::DestroyChildren (this=0x8c55c0)at ../src/common/wincmn.cpp:608
#63 0x00007ffff787ccdb in wxWindow::~wxWindow (this=0x8c55c0, __in_chrg=<optimised out>) at ../src/qt/window.cpp:170
#64 0x00007ffff783763c in wxNonOwnedWindowBase::~wxNonOwnedWindowBase (this=0x8c55c0, __in_chrg=<optimised out>) at ../include/wx/nonownedwnd.h:25
#65 0x00007ffff78376c8 in wxNonOwnedWindow::~wxNonOwnedWindow (this=0x8c55c0, __in_chrg=<optimised out>) at ../include/wx/nonownedwnd.h:108
#66 0x00007ffff798f79a in wxTopLevelWindowBase::~wxTopLevelWindowBase (this=0x8c55c0, __in_chrg=<optimised out>) at ../src/common/toplvcmn.cpp:59
#67 0x00007ffff77fc122 in wxTopLevelWindowNative::~wxTopLevelWindowNative (this=0x8c55c0, __in_chrg=<optimised out>) at ../include/wx/qt/toplevel.h:14
#68 0x00007ffff77fc18a in wxTopLevelWindow::~wxTopLevelWindow (this=0x8c55c0, __in_chrg=<optimised out>) at ../include/wx/toplevel.h:375
#69 0x00007ffff78f624f in wxFrameBase::~wxFrameBase (this=0x8c55c0, __in_chrg=<optimised out>) at ../src/common/framecmn.cpp:166
#70 0x00007ffff7813c75 in wxFrame::~wxFrame (this=0x8c55c0, __in_chrg=<optimised out>) at ../src/qt/frame.cpp:47
#71 0x000000000042f4b2 in MyFrame::~MyFrame (this=0x8c55c0, __in_chrg=<optimised out>) at ../../../samples/controls/controls.cpp:187
#72 0x000000000042f4e2 in MyFrame::~MyFrame (this=0x8c55c0, __in_chrg=<optimised out>) at ../../../samples/controls/controls.cpp:187
#73 0x00007ffff719a304 in wxAppConsoleBase::DeletePendingObjects (this=0x68bda0) at ../src/common/appbase.cpp:591
#74 0x00007ffff788e62e in wxAppBase::CleanUp (this=0x68bda0)at ../src/common/appcmn.cpp:133
#75 0x00007ffff72063e4 in wxEntryCleanup () at ../src/common/init.cpp:436
#76 0x00007ffff720671f in wxUninitialize () at ../src/common/init.cpp:555
#77 0x00007ffff7206870 in wxInitializer::~wxInitializer (this=0x7fffffffdc7e, __in_chrg=<optimised out>) at ../include/wx/init.h:109
#78 0x00007ffff7206504 in wxEntry (argc=@0x7ffff756c190: 1, argv=0x68bb60)at ../src/common/init.cpp:497
#79 0x00007ffff72065ac in wxEntry (argc=@0x7fffffffdccc: 1, argv=0x7fffffffddb8) at ../src/common/init.cpp:507
#80 0x00000000004136f0 in main (argc=1, argv=0x7fffffffddb8)at ../../../samples/controls/controls.cpp:379

Ok, this maybe is related to shortcuts (accelerators), but no, after removing them still crashing on the destructor:

Program received signal SIGSEGV, Segmentation fault.
QAbstractScrollArea::~QAbstractScrollArea (this=0x9682e0, __in_chrg=<optimised out>) at widgets/qabstractscrollarea.cpp:585
585	widgets/qabstractscrollarea.cpp: No existe el archivo o el directorio.
(gdb) bt
#0  QAbstractScrollArea::~QAbstractScrollArea (this=0x9682e0, __in_chrg=<optimised out>) at widgets/qabstractscrollarea.cpp:585
#1  0x00007ffff7883ed2 in wxQtEventSignalHandler<QScrollArea, wxWindow>::~wxQtEventSignalHandler (this=0x9682e0, __in_chrg=<optimised out>)at ../include/wx/qt/private/winevent.h:47
#2  0x00007ffff7884d6a in wxQtScrollArea::~wxQtScrollArea (this=0x9682e0, __in_chrg=<optimised out>) at ../src/qt/window.cpp:46
#3  0x00007ffff7884da2 in wxQtScrollArea::~wxQtScrollArea (this=0x9682e0, __in_chrg=<optimised out>) at ../src/qt/window.cpp:46
#4  0x00007ffff78327bc in wxQtTabWidget::currentChanged (this=0x96c640, index=0) at ../src/qt/notebook.cpp:34
#5  0x00007ffff7834b2e in QtPrivate::FunctionPointer<void (wxQtTabWidget::*)(int)>::call<QtPrivate::List<int, void>, void> (f=(void (wxQtTabWidget::*)(wxQtTabWidget * const, int)) 0x7ffff783276c <wxQtTabWidget::currentChanged(int)>, o=0x96c640, arg=0x7fffffffc460)at /usr/include/qt5/QtCore/qobjectdefs_impl.h:153
#6  0x00007ffff783499b in QtPrivate::QSlotObject<void (wxQtTabWidget::*)(int), QtPrivate::List<int, void>, void>::impl (which=1, this_=0x968700, r=0x96c640, a=0x7fffffffc460, ret=0x0) at /usr/include/qt5/QtCore/qobject_impl.h:149
#7  0x00007ffff5388d73 in call (a=0x7fffffffc460, r=0x96c640, this=0x968700)at ../../include/QtCore/../../src/corelib/kernel/qobject_impl.h:132
#8  QMetaObject::activate (sender=0x96c640, signalOffset=<optimised out>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc460) at kernel/qobject.cpp:3561
#9  0x00007ffff53897d7 in QMetaObject::activate (sender=<optimised out>, m=m@entry=0x7ffff61cf500 <QTabWidget::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc460) at kernel/qobject.cpp:3444
#10 0x00007ffff5c9fdbe in QTabWidget::currentChanged (this=<optimised out>, _t1=0) at .moc/moc_qtabwidget.cpp:303
#11 0x00007ffff5ca00cd in QTabWidgetPrivate::_q_showTab (this=<optimised out>, index=<optimised out>) at widgets/qtabwidget.cpp:749
#12 0x00007ffff5ca1e6f in QTabWidget::qt_static_metacall (_o=<optimised out>, _c=<optimised out>, _id=<optimised out>, _a=<optimised out>)at .moc/moc_qtabwidget.cpp:169
#13 0x00007ffff53892a6 in QMetaObject::activate (sender=sender@entry=0x91ca80, signalOffset=<optimised out>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc610) at kernel/qobject.cpp:3569
#14 0x00007ffff53897d7 in QMetaObject::activate (sender=sender@entry=0x91ca80, m=m@entry=0x7ffff61cf220 <QTabBar::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7fffffffc610) at kernel/qobject.cpp:3444
#15 0x00007ffff5c926ae in QTabBar::currentChanged (this=this@entry=0x91ca80, _t1=_t1@entry=0) at .moc/moc_qtabbar.cpp:308
#16 0x00007ffff5c97d98 in QTabBar::setCurrentIndex (this=this@entry=0x91ca80, index=index@entry=0) at widgets/qtabbar.cpp:1215
#17 0x00007ffff5c9b53b in QTabBar::removeTab (this=0x91ca80, index=0) at widgets/qtabbar.cpp:917
#18 0x00007ffff5ca0b08 in QTabWidgetPrivate::_q_removeTab (this=<optimised out>, index=0) at widgets/qtabwidget.cpp:755
#19 0x00007ffff5ca1e57 in QTabWidget::qt_static_metacall (_o=<optimised out>, _c=<optimised out>, _id=<optimised out>, _a=<optimised out>)at .moc/moc_qtabwidget.cpp:170
#20 0x00007ffff53892a6 in QMetaObject::activate (sender=0x8ebd50, signalOffset=<optimised out>, local_signal_index=local_signal_index@entry=1, argv=argv@entry=0x7fffffffc8d0) at kernel/qobject.cpp:3569
#21 0x00007ffff53897d7 in QMetaObject::activate (sender=<optimised out>, m=m@entry=0x7ffff61e6920 <QStackedWidget::staticMetaObject>, local_signal_index=local_signal_index@entry=1, argv=argv@entry=0x7fffffffc8d0) at kernel/qobject.cpp:3444
#22 0x00007ffff5e75b81 in QStackedWidget::widgetRemoved (this=<optimised out>, _t1=0) at .moc/moc_qstackedwidget.cpp:210
#23 0x00007ffff5e75c4b in QStackedWidget::qt_static_metacall (_o=<optimised out>, _c=<optimised out>, _id=<optimised out>, _a=<optimised out>)at .moc/moc_qstackedwidget.cpp:97

In this case, for example, the notebook is trying to process the page changed signals, but the notebook seems to be destroyed.

Interestly, why a QScrollArea is involved? (the memory seems to get weird) More strange things can happen, for example, if wxQtShortcutHandler is not removed, it will still crash like the first example, even it is not used at all (I even renamed it to wxQtShortcutHandler1 to be sure it is not instantiated in any place):

QObject::~QObject (this=0x96f540, __in_chrg=<optimised out>) at kernel/qobject.cpp:789
789	kernel/qobject.cpp: No existe el archivo o el directorio.
(gdb) bt
#0  QObject::~QObject (this=0x96f540, __in_chrg=<optimised out>) at kernel/qobject.cpp:789
#1  0x00007ffff788485f in wxQtShortcutHandler1::~wxQtShortcutHandler1 (this=0x96f540, __in_chrg=<optimised out>) at ../src/qt/window.cpp:59
#2  0x00007ffff788488e in wxQtShortcutHandler1::~wxQtShortcutHandler1 (this=0x96f540, __in_chrg=<optimised out>) at ../src/qt/window.cpp:59
Clone this wiki locally