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)
- {
- QTextEdit textEdit;
- 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-2010
Command used to compile…
I ran qmake -project, then qmake, then nmake in a folder containing only the file above (called main.cpp)
- Setting environment for using Microsoft Visual Studio 2010 x86 tools.
- 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;1
I then ran the EXE typed some text and clicked the quit button and was confronted with this…

The error says it was in one of the Qt DLLs
Any suggestions?
-tim
4 replies
#include <QtGui> int main(int argv, char **args) { QTextEdit textEdit; QVBoxLayout layout; layout.addWidget(&textEdit); layout.addWidget(&quitButton); QWidget window; window.setLayout(&layout); window.show(); return app.exec(); }
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:
That is of course the next step in the tutorial, but it will prevent the crash.
Parent QObjects will clean up any child QObjects upon destruction. See Object Trees & Ownership [doc.trolltech.com].
See the doc note [developer.qt.nokia.com] in the copy of the respective page [developer.qt.nokia.com] within Qt DevNet, it contains a fix for the crash and some other additions that are missing from the official document.
You must log in to post a reply. Not a member yet? Register here!


