Qt example crashing on exit...
-
This is the code, from http://doc.qt.nokia.com/4.7/gettingstartedqt.html
under the caption Adding a Quit button
@
#include <QtGui>
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();
}@
I run the code, it compiles and links fine, but when I click the Quit button it crashes
I compiled the latest Qt source with VS2010 with this guide: http://stackoverflow.com/questions/5601950/how-to-build-qt-for-visual-studio-2010Command used to compile...
I ran qmake -project, then qmake, then nmake in a folder containing only the file above (called main.cpp)
@
C:\Dev\Code\C++\Qt\test2>vcvarsall
Setting environment for using Microsoft Visual Studio 2010 x86 tools.C:\Dev\Code\C++\Qt\test2>qmake -project
C:\Dev\Code\C++\Qt\test2>qmake
C:\Dev\Code\C++\Qt\test2>nmake
Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved."C:\Program Files\Microsoft Visual Studio 10.0\VC\BIN\nmake.exe" -f Makefile.Release
Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.cl -c -nologo -Zm200 -Zc:wchar_t- -O2 -MD -GR -EHsc -W3 -w34100 -w34189 -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I"..\..\..\..\Lib\qt4.7.4\includ
e\QtCore" -I"........\Lib\qt4.7.4\include\QtGui" -I"........\Lib\qt4.7.4\include" -I"." -I"........\Lib\qt4.7.4\include\ActiveQt" -I"release" -I"........\Lib\qt4.7.4\mkspecs\default" -Forelease\ @C:\DOCUME~1\PATTIN~1\LOCALS~1\Temp\nmA8.tmp
main.cpp
link /LIBPATH:"c:\Dev\Lib\qt4.7.4\lib" /NOLOGO /INCREMENTAL:NO /MANIFEST /MANIFESTFILE:"release\test2.intermediate.manifest" /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='' processo
rArchitecture=''" /OUT:release\test2.exe @C:\DOCUME~1\PATTIN~1\LOCALS~1\Temp\nmA9.tmp
mt.exe -nologo -manifest "release\test2.intermediate.manifest" -outputresource:release\test2.exe;1C:\Dev\Code\C++\Qt\test2>@
I then ran the EXE typed some text and clicked the quit button and was confronted with this...
!http://img190.imageshack.us/img190/3371/unledtmlq.png(error message)!
The error says it was in one of the Qt DLLs
Any suggestions?
-tim -
[quote author="timpattinson" date="1317006034"]
@
#include <QtGui>
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();
}@
[/quote]
If I recall correctly, the stack is cleaned in reverse direction. That means window is destroyed before textEdit and quitButton. It will then try to delete the two widgets that are allocated on stack (bad thing). You could try instantiating window before those two, or throw the textEdit and quitButton onto the heap:@QTextEdit *textEdit = new QTextEdit()@
That is of course the next step in the tutorial, but it will prevent the crash.
-
OK, thankyou.
Are QObjects somehow automatically GC'ed or do i have to clean up after my code or use a special pointer class. -
Parent QObjects will clean up any child QObjects upon destruction. See "Object Trees & Ownership":http://doc.trolltech.com/latest/objecttrees.html.
-
See the "doc note":http://developer.qt.nokia.com/doc/qt-4.7/gettingstartedqt.html#comments in the "copy of the respective page":http://developer.qt.nokia.com/doc/qt-4.7/gettingstartedqt.html within Qt DevNet, it contains a fix for the crash and some other additions that are missing from the official document.