[SOLVED] Visual Studio and Example Program = Debug Assertion Failed
-
wrote on 4 Dec 2011, 23:29 last edited by
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
-
wrote on 5 Dec 2011, 03:15 last edited by
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.
-
wrote on 5 Dec 2011, 07:47 last edited by
[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.
-
wrote on 5 Dec 2011, 13:04 last edited by
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.
-
wrote on 5 Dec 2011, 15:31 last edited by
Thanks for the correction and the note, good to know.
-
wrote on 5 Dec 2011, 16:38 last edited by
I'm pretty new (as in the newest possible) in qt programming, that being said:
Using new to allocate the widgets and not using delete to clean them up won't result in memory loss?
Thanks,
Hugo Ribeira
-
wrote on 5 Dec 2011, 22:53 last edited by
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)
-
wrote on 6 Dec 2011, 10:25 last edited by
Understood,
Thank you very much,
Hugo Ribeira
4/8