Qt Memory (Mis-)Management?

Ulf's picture
Submitted by Ulf on Tue, 2007-05-29 16:32.

The following is valid C++ Qt code for Qt 3.


#include <qapplication.h>
#include <qpushbutton.h>
#include <qhbox.h>

int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton hello2("Dude!", 0);
QHBox box(0);
QPoint point(0, 0);
hello2.reparent(&box, point);
// return a.exec();
}

What does it do?


Lucky guess...

Crash!

It crashes indeed. But

It crashes indeed. But why?

The reason is that, in this very instance, the automatic Qt and C/C++ memory management features get into each others way. I actually designed the code this way after I heard a lecture about Qt and its (partially) automatic memory management.