Given the following fragments of code :
class MyDialog : public QDialog
{
...
};
MyDialog::~MyDialog()
{
qInfo() << "~MyDialog()";
}
and
// scope begins
MyDialog d;
d.setAttribute( WA_DeleteOnClose, true );
int result = d.exec();
qInfo() << "After exec";
// scope ends
I get the following output
~MyDialog()
double free or corruption (out)
Aborted (core dumped)
Without d.setAttribute( WA_DeleteOnClose, true );
everything is fine and expected.
NOTE : I know that there is no need to use the delete on close in this case as the dialog deletes when leaving the scope. I also don't need for a "better solution" etc (I've read a lot of posts on SO and Qt Centre Forum with these irrelevant answers). The question is Why the error occurs at the first time the ~QDialog()
is called ? And maybe Am I right that the error occurs at the first time the ~QDialog()
is called?
CodePudding user response:
Try this instead:
MyDialog* d = new MyDialog;
d->setAttribute( WA_DeleteOnClose, true );
int result = d->exec();
Either new QobjectDerived
or new QobjectDerived(this)
for dynamic allocation and then the dynamic deallocation could finalize the object destruction just once. That attribute WA_DeleteOnClose
set is obviously provoking internal delete
when the dialog getting closed but no second destructor call as in case with that object allocated on stack.
CodePudding user response:
You're creating an object on the stack and then try to delete it with delete
(indirectly via the WA_DeleteOnClose - flag). I don't see why you would not get a double delete then.