[Qt-interest] why these simple codes cause application crash?

lvbing lvbing lvbing1981 at gmail.com
Tue Apr 20 09:00:01 CEST 2010


the create and destroy order in your code is:
create the QLabel label first,then create the QWidget window;
then destroy the window first and destroy the label later.

and the problem is:
you set the label's parent,
and according to the memory manager mechanism in Qt
when you destroy the parent object,ant also destroy the children.
so destroy the window,and also destroy the label behind the code,
you can see the source code in QObject;
and then destroy the label,the memory is wrong,so your problem crashed.

and you also can make a test like this:
QLabel* label = new QLabel();
QWidget* window = new QWidget();
//
test 1:
delete label;
delete window;

test 2;
label->setParent(window);
delete label;
delete window;

test 3:
label->setParent(window);
delete window;
delete label;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100420/b0d52322/attachment.html 


More information about the Qt-interest-old mailing list