[SOLVED] Visual Studio and Example Program = Debug Assertion Failed
-
Hello guys,
I've just built qt wi vs 2010 pro. Everything seems to be running fine except that when I run this exemple:
@#include <QtGui>
#include <qtextedit.h>
int main(int argv, char **args)
{
QApplication app(argv, args);QTextEdit textEdit;
QPushButton quitButton("Quit");QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
QVBoxLayout layout;
layout.addWidget(&textEdit);
layout.addWidget(&quitButton);QWidget window;
window.setLayout(&layout);window.show();
return app.exec();
}@As soon as I hit quitButton the debugger kicks in with an Debug Assertion Failed.
Anyone had this problem?
Thanks,
Hugo Ribeira
-
That problem occurs due to a memory cleanup mess-up by your compiler. Qt encourages you to always allocate any QWidget and QWidget-derived object (QPushButton, QTextEdit, etc.) dynamically, with operator new.
@#include <QtGui>
int main(int argv, char **args)
{
QApplication app(argv, args);QTextEdit* textEdit = new QTextEdit();
QPushButton* quitButton = new QPushButton("Quit");QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
QVBoxLayout layout;
layout.addWidget(textEdit);
layout.addWidget(quitButton);QWidget* window = new QWidget();
window->setLayout(&layout);window->show();
return app.exec();
}@I removed the line
@#include <qtextedit.h>@
since QtGui includes all of the GUI's classes.
-
[quote author="veeeee_d" date="1323054904"]That problem occurs due to a memory cleanup mess-up by your compiler. Qt encourages you to always allocate any QWidget and QWidget-derived object (QPushButton, QTextEdit, etc.) dynamically, with operator new.
[/quote]This is not 100% correct, so one correction:
Qt cleans up all child elements of any QObject explicitly with delete when the parent object is deleted. So, top levvel objects are not cleand up, in you code, that would be
@
QWidget* window = new QWidget();
@so the correct code would be:
@
#include <QtGui>int main(int argv, char **args)
{
QApplication app(argv, args);QWidget window; QTextEdit* textEdit = new QTextEdit(&window); QPushButton* quitButton = new QPushButton("Quit", &window); QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); QVBoxLayout layout; window->setLayout(&layout); layout.addWidget(textEdit); layout.addWidget(quitButton); window->show(); return app.exec();
}
@I also suggest to create the parent before adding the widgets to the layout, and to give the parent in the C#tor as it makes it clearer, where an object lives.
-
See the "DocNote":http://developer.qt.nokia.com/doc/qt-4.7/gettingstartedqt.html#note-22 in the DevNet version of that page for corrections and other missing code of that guide.
-
The story is a bit more detailed:
In principle you must delete a widget that you have created with new. There is an exception to this rule: If you create a widget with a parent (or put it in a layout, which will give it a parent too), you do not need to delete it manually, as the parent widget deletes its children on its own destruction. So, in practice you will usually only need to delete the toplevel widget (dialog, mainwindow, etc)