September 26, 2011

timpattinson timpattinson
Lab Rat
4 posts

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

  1. #include <QtGui>
  2. int main(int argv, char **args)
  3. {
  4.     QApplication app(argv, args);
  5.  
  6.     QTextEdit textEdit;
  7.     QPushButton quitButton("Quit");
  8.  
  9.      QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
  10.  
  11.      QVBoxLayout layout;
  12.      layout.addWidget(&textEdit);
  13.      layout.addWidget(&quitButton);
  14.  
  15.      QWidget window;
  16.      window.setLayout(&layout);
  17.  
  18.      window.show();
  19.  
  20.      return app.exec();
  21.  }

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)

  1. C:\Dev\Code\C++\Qt\test2>vcvarsall
  2. Setting environment for using Microsoft Visual Studio 2010 x86 tools.
  3.  
  4. C:\Dev\Code\C++\Qt\test2>qmake -project
  5.  
  6. C:\Dev\Code\C++\Qt\test2>qmake
  7.  
  8. C:\Dev\Code\C++\Qt\test2>nmake
  9.  
  10. Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
  11. Copyright (C) Microsoft Corporation.  All rights reserved.
  12.  
  13.         "C:\Program Files\Microsoft Visual Studio 10.0\VC\BIN\nmake.exe" -f Makefile.Release
  14.  
  15. Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
  16. Copyright (C) Microsoft Corporation.  All rights reserved.
  17.  
  18.         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
  19. 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
  20. main.cpp
  21.         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
  22. rArchitecture='*'" /OUT:release\test2.exe @C:\DOCUME~1\PATTIN~1\LOCALS~1\Temp\nmA9.tmp
  23.         mt.exe -nologo -manifest "release\test2.intermediate.manifest" -outputresource:release\test2.exe;1
  24.  
  25. C:\Dev\Code\C++\Qt\test2>

I then ran the EXE typed some text and clicked the quit button and was confronted with this…
error message
The error says it was in one of the Qt DLLs
Any suggestions?
-tim

4 replies

September 26, 2011

Franzk Franzk
Lab Rat
836 posts

timpattinson wrote:

  1. #include <QtGui>
  2. int main(int argv, char **args)
  3. {
  4.     QApplication app(argv, args);
  5.  
  6.     QTextEdit textEdit;
  7.     QPushButton quitButton("Quit");
  8.  
  9.      QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
  10.  
  11.      QVBoxLayout layout;
  12.      layout.addWidget(&textEdit);
  13.      layout.addWidget(&quitButton);
  14.  
  15.      QWidget window;
  16.      window.setLayout(&layout);
  17.  
  18.      window.show();
  19.  
  20.      return app.exec();
  21.  }


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:

  1. QTextEdit *textEdit = new QTextEdit()

That is of course the next step in the tutorial, but it will prevent the crash.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

September 26, 2011

timpattinson timpattinson
Lab Rat
4 posts

OK, thankyou.
Are QObjects somehow automatically GC’ed or do i have to clean up after my code or use a special pointer class.

September 26, 2011

Franzk Franzk
Lab Rat
836 posts

Parent QObjects will clean up any child QObjects upon destruction. See Object Trees & Ownership [doc.trolltech.com].

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

September 26, 2011

Volker Volker
Ant Farmer
5428 posts

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.

 
  ‹‹ [Solved] ’Distributing’ area between two widgets, and resizing that area?      Invalid Socket 0 and type ’read’ and disabling ››

You must log in to post a reply. Not a member yet? Register here!