Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
double free or corruption (out):
ChajusSaib last edited by
Hello lads, so I've just started reading C++ GUI Programming with Qt4.2 2nd edition and in in chapter 2 a bit of the the constructor is like this:
QHBoxLayout *topLeftLayout = new QHBoxLayout; topLeftLayout->addWidget(label); topLeftLayout->addWidget(lineEdit); QVBoxLayout *leftLayout = new QVBoxLayout; leftLayout->addLayout(topLeftLayout); leftLayout->addWidget(caseCheckBox); leftLayout->addWidget(backwardCheckBox); QVBoxLayout *rightLayout = new QVBoxLayout; rightLayout->addWidget(findButton); rightLayout->addWidget(closeButton); rightLayout->addStretch(); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(leftLayout); mainLayout->addLayout(rightLayout); setLayout(mainLayout);
This gives me no errors and works but the problem but I do not understand why, how does all this data located get freed?
I try run this: (notice that I do no dynamically locate the objects)
QHBoxLayout topLeftLayout; topLeftLayout.addWidget(label); topLeftLayout.addWidget(lineEdit); QVBoxLayout leftLayout; leftLayout.addLayout(&topLeftLayout); leftLayout.addWidget(caseCheckBox); leftLayout.addWidget(backwardCheckBox); QVBoxLayout rightLayout; rightLayout.addWidget(findButton); rightLayout.addWidget(closeButton); rightLayout.addStretch(); QHBoxLayout mainLayout; mainLayout.addLayout(&leftLayout); mainLayout.addLayout(&rightLayout); setLayout(&mainLayout);
And I get
*** Error in `./findDialog': double free or corruption (out): 0x00007ffde0c74010 ***
Why is this, another question is this class dynamically allocates all of its widgets and does not have a destructor that frees them. In the book it does specify that the OS will claim it back but isn't that bad practice.
If Qt is freeing my data for me I don't think I like it because I don't know if I should free something or not. Maybe I'll get used to it as I progress. Cheers!
You have everything explained here
ChajusSaib last edited by
Thanks for the quick reply, I still do not fully understand what happens in the constructor, what is happening when they are stored on the stack instead of the heap?
As for the second question how I do not tell them that the class(FindDialog) is their parent. Like say label is just a a QLabel initialized as follows:
label(new QLabel(tr("Find &what:")))
How does it know FindDialog is its parent?
That's basic C++: topLeftLayout, leftLayout, rightLayout and mainLayout all goes out of scope at then end of the constructor so they get destroyed.
Now on to the Qt part: topLeftLayout is a child of leftLayout and leftLayout and rightLayout have become children of mainLayout so the destruction of mainLayout will also trigger the destruction of leftLayout and rightLayout hence your double free problem.
You put label in topLeftLayout that you add to leftLayout that you add to mainLayout that you set on your widget.
ChajusSaib last edited by ChajusSaib
@SGaist Thank you mate, that clears it up a lot!
EDIT: Is there a way for me to know when I should free something? Or will it just come by practice?