Solved How do I delete the current QTreeWidget element correctly?
-
Q_ASSERT(parentItem);
For some reason, I have a crash with this command, and there is no decryption of the ASSERT error
When compiled for debug, failure on this line should simply show you which line (probably including the word
parentItem
) it failed on. That's whatQ_ASSERT()
does.Q_ASSERT(parentItem);
in your code will correctly assert, when the item being deleted has no parent (i.e. top-level). Therefore your code should indeed not have that line. But the line I said it could have is the commented-out//Q_ASSERT(takenItem==item);
, which should succeed logic-wise. This is a detail.Please tell me an alternative to the command
ui_selectpreset->treeWidget->takeTopLevelItem(index);
to avoid leakage.
You wouldn't want me to tell you, because you know this. When deleting a non-top-level element you already have
QTreeWidgetItem *takenItem = parentItem->takeChild(parentItem->indexOfChild(item)); Q_ASSERT(takenItem==item); delete takenItem;
So what do you think you have correspondingly in the
takeTopLevelItem()
place? -
I replaced the line by analogy
ui_selectpreset->treeWidget->takeTopLevelItem(index);
with the following lines:
QTreeWidgetItem *takenItem = ui_selectpreset->treeWidget->takeTopLevelItem(ui_selectpreset->treeWidget->indexOfTopLevelItem(item)); Q_ASSERT(takenItem==item); delete takenItem;
(The only thing is that I couldn't start Q_ASSERT, I already turned everything on and still it doesn't work for me.)
-
@Helg1980 said in How do I delete the current QTreeWidget element correctly?:
The only thing is that I couldn't start Q_ASSERT
I have no idea what you mean by this. What does "start" mean here?
Q_ASSERT()
is a macro, producing code during compilation.and still it doesn't work for me.
In what way "work" or "doesn't work"?
BTW, your Build Settings are not relevant to this.
-
I mean, Q_ASSERT does not output a message in which line the crash occurred (in the case of Q_ASSERT(parentItem);)
Therefore, I concluded that this command does not work for me.. -
@Helg1980
Are you compiling for Debug or Release?Q_ASSERT()
does nothing when compiled for Release, only for Debug. -
Compilation I have in Debud mode, here is my compilation command:
cine_encoder.pro -spec linux-g++ CONFIG+=debug CONFIG+=force_debug_info CONFIG+=separate_debug_info && /usr/bin/make qmake_all
-
@Helg1980
That's not actually any kind of compiler command, just a make-type one, but never mind.I really don't know. If the first line of your program is a
Q_ASSERT(false)
it should produce an error (when run) if compiled for Debug. And if you run your application from the debugger it should cause that to break on the offending line. If it'sQ_ASSERT(true)
it should do precisely nothing. You might test with these two.P.S.
Where are you looking for the message to appear? If running from Qt Creator I think it would go to the Application Output pane. -
@Helg1980 said in How do I delete the current QTreeWidget element correctly?:
Q_ASSERT(parentItem);
That's the point, that in Q_ASSERT(parentItem), the value 'parrentItem' is false, but Q_ASSERT does not give a message about this, but simply crashes the program;
-
Yes, I work in QtCreator.
Here is my Application Output:18:31:38: Starting /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder ... 18:31:53: The program has unexpectedly finished. 18:31:53: The process was ended forcefully. 18:31:53: /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder crashed.
-
@Helg1980
OK, I don't know what you are doing, and what your program is doing.It is time now to use the debugger to put a breakpoint in your program, and step through offending code. Besides, I don't know how you are getting a "crashed" without the debugger catching it and showing you a stack trace back to the faulting line. If you are not familiar with using the debugger it's time you were, as you can resolve these issues very simply by using it!
-
@Helg1980
Start the debugger. If you have a debugger in the background it will tell you where the assertion occurred.Q_ASSERT
andQ_ASSUME
are used to test implicit assumptions you make in your code.Looking at the sources
QTreeWidget
it looked likeitem->parent()
for a top level item would return the invisible root item. I tested this implicit assumption withQ_ASSERT
and it turns out I was wrong (but at least we know why)void SelectPreset::on_actionRemove_preset_clicked() // Remove preset { QTreeWidgetItem *item = ui_selectpreset->treeWidget->currentItem(); if(!item) return; QTreeWidgetItem *parentItem = item->parent(); QTreeWidgetItem *takenItem = nullptr; if(parentItem) takenItem = parentItem->takeChild(parentItem->indexOfChild(item)); else takenItem = ui_selectpreset->treeWidget->takeTopLevelItem(ui_selectpreset->treeWidget->indexOfTopLevelItem(item)); Q_ASSERT(takenItem==item); delete takenItem; }
-
Finally, I finished the program and ran it through 'valgrind'. The result was disappointing:
valgrind --leak-check=full --leak-resolution=med /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder . . . ==2615== HEAP SUMMARY: ==2615== in use at exit: 568,907 bytes in 7,703 blocks ==2615== total heap usage: 880,287 allocs, 872,584 frees, 5,593,630,039 bytes allocated ==2615== ==2615== 24 bytes in 1 blocks are definitely lost in loss record 89 of 557 ==2615== at 0x483A77F: malloc (vg_replace_malloc.c:307) ==2615== by 0xDE61E5E: XextCreateExtension (in /usr/lib/libXext.so.6.4.0) ==2615== by 0x1074014B: ??? ==2615== by 0x1073775C: ??? ==2615== by 0x10662091: ??? (in /usr/lib/qt/plugins/xcbglintegrations/libqxcb-glx-integration.so) ==2615== by 0xA28C084: QXcbWindow::create() (in /usr/lib/libQt5XcbQpa.so.5.15.1) ==2615== by 0xA27A746: QXcbIntegration::createPlatformWindow(QWindow*) const (in /usr/lib/libQt5XcbQpa.so.5.15.1) ==2615== by 0x5842F26: QWindowPrivate::create(bool, unsigned long long) (in /usr/lib/libQt5Gui.so.5.15.1) ==2615== by 0x51F78A7: QWidgetPrivate::create() (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x51F7E20: QWidget::create(unsigned long long, bool, bool) (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x520551B: QWidgetPrivate::setVisible(bool) (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x117E3F: main (main.cpp:10) ==2615== ==2615== 96 (64 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 316 of 557 ==2615== at 0x483ADEF: operator new(unsigned long) (vg_replace_malloc.c:342) ==2615== by 0x159CCB: SelectPreset::SelectPreset(QWidget*) (selectpreset.cpp:28) ==2615== by 0x129258: MainWindow::on_actionPreset_clicked() (mainwindow.cpp:1279) ==2615== by 0x1659B3: MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) (moc_mainwindow.cpp:156) ==2615== by 0x165B5F: MainWindow::qt_metacall(QMetaObject::Call, int, void**) (moc_mainwindow.cpp:204) ==2615== by 0x60B5D71: ??? (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0x52B5B22: QAbstractButton::clicked(bool) (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x52B64AB: ??? (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x52B7E52: ??? (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x52B8032: QAbstractButton::mouseReleaseEvent(QMouseEvent*) (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x5205B0D: QWidget::event(QEvent*) (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== by 0x51C4751: QApplicationPrivate::notify_helper(QObject*, QEvent*) (in /usr/lib/libQt5Widgets.so.5.15.1) ==2615== ==2615== 160 bytes in 1 blocks are possibly lost in loss record 369 of 557 ==2615== at 0x483A77F: malloc (vg_replace_malloc.c:307) ==2615== by 0x60A8595: QObjectPrivate::addConnection(int, QObjectPrivate::Connection*) (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0x60AC9DE: QObjectPrivate::connectImpl(QObject const*, int, QObject const*, void**, QtPrivate::QSlotObjectBase*, Qt::ConnectionType, int const*, QMetaObject const*) (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0x60ACEB5: QObject::connectImpl(QObject const*, void**, QObject const*, void**, QtPrivate::QSlotObjectBase*, Qt::ConnectionType, int const*, QMetaObject const*) (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0xA3D1BBD: QDBusConnectionInterface::QDBusConnectionInterface(QDBusConnection const&, QObject*) (in /usr/lib/libQt5DBus.so.5.15.1) ==2615== by 0xA3CEB3A: ??? (in /usr/lib/libQt5DBus.so.5.15.1) ==2615== by 0xA3CFD13: ??? (in /usr/lib/libQt5DBus.so.5.15.1) ==2615== by 0x60AB7E1: QObject::event(QEvent*) (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0x607ECAF: QCoreApplication::notifyInternal2(QObject*, QEvent*) (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0x60817D2: QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0x60D8303: ??? (in /usr/lib/libQt5Core.so.5.15.1) ==2615== by 0x6A2CBFB: g_main_context_dispatch (in /usr/lib/libglib-2.0.so.0.6600.0) ==2615== ==2615== 302 (256 direct, 46 indirect) bytes in 1 blocks are definitely lost in loss record 409 of 557 ==2615== at 0x483A77F: malloc (vg_replace_malloc.c:307) ==2615== by 0xA38F9E2: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==2615== by 0xA3902E4: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==2615== by 0xA391BEB: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==2615== by 0xA399E59: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==2615== by 0xA5309E7: ??? (in /usr/lib/libexpat.so.1.6.11) ==2615== by 0xA52EDDC: ??? (in /usr/lib/libexpat.so.1.6.11) ==2615== by 0xA532620: ??? (in /usr/lib/libexpat.so.1.6.11) ==2615== by 0xA5348CB: XML_ParseBuffer (in /usr/lib/libexpat.so.1.6.11) ==2615== by 0xA397024: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==2615== by 0xA397657: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==2615== by 0xA397713: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==2615== ==2615== LEAK SUMMARY: ==2615== definitely lost: 344 bytes in 3 blocks ==2615== indirectly lost: 78 bytes in 3 blocks ==2615== possibly lost: 160 bytes in 1 blocks ==2615== still reachable: 568,305 bytes in 7,694 blocks ==2615== of which reachable via heuristic: ==2615== newarray : 4,264 bytes in 1 blocks ==2615== suppressed: 20 bytes in 2 blocks ==2615== Reachable blocks (those to which a pointer was found) are not shown. ==2615== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==2615== ==2615== Use --track-origins=yes to see where uninitialised values come from ==2615== For lists of detected and suppressed errors, rerun with: -s ==2615== ERROR SUMMARY: 107 errors from 107 contexts (suppressed: 0 from 0)
-
@Helg1980 said in How do I delete the current QTreeWidget element correctly?:
The result was disappointing:
Why? There is only one leak which should interest you:
==2615== by 0x159CCB: SelectPreset::SelectPreset(QWidget*) (selectpreset.cpp:28)
The rest are no real 'leaks' and can be ignored.
-
Here is a part of the text where there may be an error:
SelectPreset::SelectPreset(QWidget *parent) : QDialog(parent), ui_selectpreset(new Ui::SelectPreset) { ui_selectpreset->setupUi(this); ui_selectpreset->treeWidget->clear(); int n = _preset_table[0].size(); int m = _preset_table.size(); QString type; QColor color; color.setRgba(qRgb(120, 120, 150)); QFont font; font.setBold(true); QTreeWidgetItem *item = new QTreeWidgetItem(); // line 28 - deleted this line for (int i = 0; i < n; i++) { type = _preset_table[23][i]; if (type == "TopLewelItem") { QTreeWidgetItem *root = new QTreeWidgetItem(); root->setText(0, _preset_table[0][i]); ui_selectpreset->treeWidget->addTopLevelItem(root); ui_selectpreset->treeWidget->setCurrentItem(root); root->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); root->setFont(0, font); root->setForeground(0, color); }; if (type == "ChildItem") { item = ui_selectpreset->treeWidget->currentItem(); // replaced with: QTreeWidgetItem *item = .... QTreeWidgetItem *child = new QTreeWidgetItem(); for (int j = 0; j < 23; j++) { child->setText(j, _preset_table[j][i]); }; child->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); item->addChild(child); }; }; item = ui_selectpreset->treeWidget->topLevelItem(pos_top)->child(pos_cld); // replaced with: QTreeWidgetItem *item = .... ui_selectpreset->treeWidget->setCurrentItem(item); std::cout << n << " x " << m << std::endl; // Table size ui_selectpreset->treeWidget->expandAll(); for (int i=1; i<=24; i++) { ui_selectpreset->treeWidget->hideColumn(i); }; QAction *addsection = new QAction(tr("Add section"), this); QAction *addpreset = new QAction(tr("Add new preset"), this); connect(addsection, &QAction::triggered, this, &SelectPreset::add_section); connect(addpreset, &QAction::triggered, this, &SelectPreset::add_preset); QMenu* menu = new QMenu(this); menu->addAction(addsection); menu->addAction(addpreset); menu->setStyleSheet("QMenu {background-color: rgb(5, 20, 25);} " "QMenu::item {background-color: transparent;} " "QMenu::item:selected {background-color: rgb(5, 40, 45);}"); ui_selectpreset->actionAdd_preset->setMenu(menu); }
-
@Helg1980 said in How do I delete the current QTreeWidget element correctly?:
QTreeWidgetItem *item = new QTreeWidgetItem(); // line 28
So, as the
valgrind
tells you, in all cases you do not use/overwrite thisnew
editem
without using it. Hence the leak. -
This post is deleted! -
@Helg1980
So I see. So now has that leak gone away? That's all you are looking to resolve. -
It looks like the error has disappeared. I don't work very well with valgrind yet. So I understand that I only need to search for lines where there are links to my files.
==7099== HEAP SUMMARY: ==7099== in use at exit: 571,790 bytes in 7,682 blocks ==7099== total heap usage: 833,977 allocs, 826,295 frees, 5,575,110,174 bytes allocated ==7099== ==7099== 24 bytes in 1 blocks are definitely lost in loss record 94 of 561 ==7099== at 0x483A77F: malloc (vg_replace_malloc.c:307) ==7099== by 0xDE61E5E: XextCreateExtension (in /usr/lib/libXext.so.6.4.0) ==7099== by 0x1074014B: ??? ==7099== by 0x1073775C: ??? ==7099== by 0x10662091: ??? (in /usr/lib/qt/plugins/xcbglintegrations/libqxcb-glx-integration.so) ==7099== by 0xA28C084: QXcbWindow::create() (in /usr/lib/libQt5XcbQpa.so.5.15.1) ==7099== by 0xA27A746: QXcbIntegration::createPlatformWindow(QWindow*) const (in /usr/lib/libQt5XcbQpa.so.5.15.1) ==7099== by 0x5842F26: QWindowPrivate::create(bool, unsigned long long) (in /usr/lib/libQt5Gui.so.5.15.1) ==7099== by 0x51F78A7: QWidgetPrivate::create() (in /usr/lib/libQt5Widgets.so.5.15.1) ==7099== by 0x51F7E20: QWidget::create(unsigned long long, bool, bool) (in /usr/lib/libQt5Widgets.so.5.15.1) ==7099== by 0x520551B: QWidgetPrivate::setVisible(bool) (in /usr/lib/libQt5Widgets.so.5.15.1) ==7099== by 0x117E3F: main (main.cpp:10) ==7099== ==7099== 160 bytes in 1 blocks are possibly lost in loss record 372 of 561 ==7099== at 0x483A77F: malloc (vg_replace_malloc.c:307) ==7099== by 0x60A8595: QObjectPrivate::addConnection(int, QObjectPrivate::Connection*) (in /usr/lib/libQt5Core.so.5.15.1) ==7099== by 0x60AC9DE: QObjectPrivate::connectImpl(QObject const*, int, QObject const*, void**, QtPrivate::QSlotObjectBase*, Qt::ConnectionType, int const*, QMetaObject const*) (in /usr/lib/libQt5Core.so.5.15.1) ==7099== by 0x60ACEB5: QObject::connectImpl(QObject const*, void**, QObject const*, void**, QtPrivate::QSlotObjectBase*, Qt::ConnectionType, int const*, QMetaObject const*) (in /usr/lib/libQt5Core.so.5.15.1) ==7099== by 0xA3D1BBD: QDBusConnectionInterface::QDBusConnectionInterface(QDBusConnection const&, QObject*) (in /usr/lib/libQt5DBus.so.5.15.1) ==7099== by 0xA3CEB3A: ??? (in /usr/lib/libQt5DBus.so.5.15.1) ==7099== by 0xA3CFD13: ??? (in /usr/lib/libQt5DBus.so.5.15.1) ==7099== by 0x60AB7E1: QObject::event(QEvent*) (in /usr/lib/libQt5Core.so.5.15.1) ==7099== by 0x607ECAF: QCoreApplication::notifyInternal2(QObject*, QEvent*) (in /usr/lib/libQt5Core.so.5.15.1) ==7099== by 0x60817D2: QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) (in /usr/lib/libQt5Core.so.5.15.1) ==7099== by 0x60D8303: ??? (in /usr/lib/libQt5Core.so.5.15.1) ==7099== by 0x6A2CBFB: g_main_context_dispatch (in /usr/lib/libglib-2.0.so.0.6600.0) ==7099== ==7099== 302 (256 direct, 46 indirect) bytes in 1 blocks are definitely lost in loss record 414 of 561 ==7099== at 0x483A77F: malloc (vg_replace_malloc.c:307) ==7099== by 0xA38F9E2: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==7099== by 0xA3902E4: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==7099== by 0xA391BEB: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==7099== by 0xA399E59: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==7099== by 0xA5309E7: ??? (in /usr/lib/libexpat.so.1.6.11) ==7099== by 0xA52EDDC: ??? (in /usr/lib/libexpat.so.1.6.11) ==7099== by 0xA532620: ??? (in /usr/lib/libexpat.so.1.6.11) ==7099== by 0xA5348CB: XML_ParseBuffer (in /usr/lib/libexpat.so.1.6.11) ==7099== by 0xA397024: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==7099== by 0xA397657: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==7099== by 0xA397713: ??? (in /usr/lib/libfontconfig.so.1.12.0) ==7099== ==7099== LEAK SUMMARY: ==7099== definitely lost: 280 bytes in 2 blocks ==7099== indirectly lost: 46 bytes in 2 blocks ==7099== possibly lost: 160 bytes in 1 blocks ==7099== still reachable: 571,284 bytes in 7,675 blocks ==7099== of which reachable via heuristic: ==7099== newarray : 4,264 bytes in 1 blocks ==7099== suppressed: 20 bytes in 2 blocks ==7099== Reachable blocks (those to which a pointer was found) are not shown. ==7099== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==7099== ==7099== Use --track-origins=yes to see where uninitialised values come from ==7099== For lists of detected and suppressed errors, rerun with: -s ==7099== ERROR SUMMARY: 80 errors from 80 contexts (suppressed: 0 from 0)
-
@Helg1980
Are you runningvalgrind
yourself with those command-line arguments? I run it from inside Qt Creator, from the menu item. I (Ubuntu 20.04) always get exactly 32 leaks, which with the default of the checkbox which says something about "don't report system ones" being checked, are always suppressed, so I don't see them. Any genuine ones from my own code are then the only ones reported. -
This post is deleted!