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

Edward marslee_suc at 163.com
Tue Apr 20 07:12:13 CEST 2010


Hi, Qters

I found below codes in some Qt document. It says that these codes can cause application crash. I tried these codes and it crashed as expected, the document told me that the label widget delete twice, I understand it but I don't know detail information about it. I checked Qt source code of QObject,but I can not find the reason. Any body know detail information about that crash? could you help me point out the reason in soure code.Thank you so much. 
"
#include <QtGui>
#include <QApplication>
int main(int argc, char* argv[])
  {
  QApplication app(argc, argv);
  QLabel label("Helloworld");
  QWidget window;
   label.setParent(&window);
  window.show();
  return app.exec();
  }
"

BTW: according to my understanding I wrote below test codes, but these doesn't crash:(

#include <QtCore/QCoreApplication>
#include <stdlib.h>
class A
{
public:
    A();
    ~A();
    void set(A *a);
private:
    A *i;
    int *a;
};

A::A()
{
    a=(int*)malloc(100);
    i = NULL;
}

A::~A()
{
    if (a)
    {
        free(a);
        a=NULL;
    }
    if (i)
    {
        delete i;
        i=NULL;
    }
}

void A::set(A *a)
{
    i = a;
}


void test()
{

    A test1;
    A test2;
    test2.set(&test1);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    test();

    return a.exec();
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100420/369fcb38/attachment.html 


More information about the Qt-interest-old mailing list