Debug assertion fail when nesting QSplitters in QWidget
-
Debug Assertion Failed!
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)This happens when you nest QSplitters in the wrong way. Anyone know why? Here's a simple example that recreates the problem
brokenwidget.hpp
@#include <QPlainTextEdit>
#include <QSplitter>
class brokenwidget : public QWidget{
Q_OBJECT
public:
brokenwidget(QWidget *parent = NULL);
QPlainTextEdit jnk1, jnk2, jnk3;
QSplitter middle, bottom;
};@brokenwidget.cpp
@#include "brokenwidget.hpp"
brokenwidget::brokenwidget(QWidget *parent){
middle.addWidget(&jnk1);
middle.addWidget(&jnk2);
bottom.addWidget(&middle);
bottom.addWidget(&jnk3);
}@main.cpp
@#include "brokenwidget.hpp"
int main(int argv, char **args){
QApplication app(argv, args);
brokenwidget wid = brokenwidget();
wid.show();
return app.exec();
}@ -
Hi, and welcome to the Qt Dev Net!
The crash happened because your widgets got deleted twice.
To avoid this problem, allocate your member variables on the heap using new. See "Object Trees & Ownership":http://qt-project.org/doc/qt-5/objecttrees.html for details.
-
When is this happening ? Is it happening when you close the window ? Here when close the window, it is trying to freeup all the child widgets of splitter. Since they are not allocated on the heap, it is getting into this problem. QSplitter has no way of knowing whether it is allocated on heap or member variable. It may be hitting this issue.
Also what is wrong of nesting here ? I did not see any issue with that.
Also you can do the following
@QPlainTextEdit *edit = new QPlainTextEdit;
QPlainTextEdit *edit1 = new QPlainTextEdit;
QPlainTextEdit *edit2 = new QPlainTextEdit;QSplitter *top = new QSplitter(this); top->addWidget(edit); top->addWidget(edit1); QSplitter *bottom = new QSplitter(this); top->setOrientation(Qt::Vertical); top->addWidget(bottom); top->addWidget(edit2);
@
-
Oh. Thanks for the tip
I try to avoid heap allocation. And for some reason it seems like every example program is full of memory leaks because nobody saves their pointers and deletes them later.
Does Qt have some sort of garbage collection for QWidgets?
Why are they being deleted twice? Shouldn't each individual object's destructor handle itself?
I don't understand why this should happen at all. Which ones specifically get deleted twice, when, and why? -
Hi,
It's all explained in QObject's "documentation":http://qt-project.org/doc/qt-5/qobject.html#details
-
[quote]Does Qt have some sort of garbage collection for QWidgets?[/quote]Yes. Read the links that SGaist and I gave you.
They get deleted once by the garbage collector. Then, they get deleted again when they get removed from the "stack". That's why you should use the heap instead.