June 18, 2012

romeo.rbw romeo.rbw
Ant Farmer
77 posts

Unhandled - Thread

 

Hello Qt-ers,

I tried using a Thread. The function in Thread already loaded successfully by the program, but there is a dialog box appeared if the condition to enter the thread provided by user, it could many times display (2 times if thread twice called, or 10 times is thread 10 times called, etc). The dialog Box is a Visual Studio Just-In-Time Debugger, the message is “An unhandled Win32 exception occured in mythread.exe [6536]”.
But the application still running. Would you help me to explain or give me some suggestion? Here is my method of thread, I follow from one of the youtube Qt video tutorial but modified and implemented it for my application. Sorry about my English.

in mainwindow.cpp

  1.     QThread  cThread;
  2.     MyObject  cObject;
  3.  
  4.     cObject.DoSetup(cThread);
  5.     cObject.moveToThread(&cThread);
  6.  
  7.     if (X3 >=415 && X3<=470 && Y3 >=225 && Y3<=238){  //my condition to enter thread
  8.         cThread.start();
  9.  
  10.         qDebug() << "finish" ;
  11.  
  12.     }

in myobject.h

  1. #ifndef MYOBJECT_H
  2. #define MYOBJECT_H
  3.  
  4. #include <QObject>
  5. #include <QThread>
  6. #include <QDebug>
  7.  
  8. class MyObject : public QObject
  9. {
  10.     Q_OBJECT
  11. public:
  12.     explicit MyObject(QObject *parent = 0);
  13.     void DoSetup(QThread &cThread);
  14.    
  15. signals:
  16.    
  17. public slots:
  18.     void DoWork();
  19. };
  20. #endif // MYOBJECT_H

in myobject.cpp

  1. #include "myobject.h"
  2.  
  3. MyObject::MyObject(QObject *parent) :
  4.     QObject(parent)
  5. {
  6. }
  7.  
  8. void MyObject::DoSetup(QThread &cThread)
  9. {
  10.     connect(&cThread,SIGNAL(started()),
  11.             this,SLOT(DoWork()));
  12. }
  13.  
  14. void MyObject::DoWork()
  15. {
  16.     for(int i=0 ; i<20 ; i++){
  17.         qDebug() << i;
  18.     }
  19. }

Thanks in advance.

-romeo

5 replies

June 18, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Your cThread object is most probably going out of scope before the thread has finished its operation (and thus the thread is destroyed while it is still running).

Apart from that your example should work as expected.

Have you tried running your application in debug mode to see where it actually crashes?

June 19, 2012

romeo.rbw romeo.rbw
Ant Farmer
77 posts

Thanks for the reply..
You are right there is a message about Thread was destroyed in my application output. How to fix that and what does it mean?

I used “Qt 4.8.0 for Desktop-MSVC2010 Release” before. The application running. But I try to change to “Qt 4.8.0 for Desktop-MSVC2010 Debug” mode, but after I hit Ctrl-R there was a dialog box Microsoft Visual C++ Runtime Library, the message is Program:..MSVC2010_Qt_SDK_Debug\debug\mythread.exe abnormal program termination.

When I hit Start Debug in the toolbar, there was two Qt Warning dialog boxes. The messages are:

- Message in the second dialog box. This does not seem to be a “Debug” build. Setting breakpoints by file name and line number may fail.

- Message in the first dialog box. The preferred debugger engine for debugging binaries of type ‘x86-windows-msvc2010-pe-32bit’ is not available. The debugger engine ‘Gdb engine’ will be used as a fallback. Details: There is no CDB library availble for binaries in format ‘x86-windows-msvc2010-pe-32bit’

Do you have any suggestion to fix that so I cannot debug now.
Thank you in advance.
-romeo

June 19, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Did you rebuild your application after switching from release to debug?

For MSVC you should have CDB installed. See QtCreator: Setting Up Debugger [doc.qt.nokia.com].

June 20, 2012

romeo.rbw romeo.rbw
Ant Farmer
77 posts

Yes, I rebuild the application.
It is difficult to find CDB instalation for MSVC, I only found something like WSK-SDK from Microsoft web and could not install in my XP windows.
Is there a suggestion to handle a message about ‘Thread was destroyed while it is running’ in my application output? I think if I can handle it may be the problem solved.
Thank in advance.
-romeo

June 20, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Yes, create your thread object at a larger scope, for example as a member of your class or on the heap, so it won’t go out of scope (and thus beeing destroyed) before your thread has finished execution.

 
  ‹‹ Qt Creator problem      return of uint QDir::count () const ››

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