Results 1 to 6 of 6

Thread: Qt application output says crashed

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Qt application output says crashed

    I have written a small console application that prints something on QTimer timeout. I have a counter, which emits a signal to stop the QTimer when it reaches a predetermined value. Everything looks fine, except the application output says crashed when I close the console.

    Qt Code:
    1. // main.cpp
    2. #include <QCoreApplication>
    3. #include <QDebug>
    4. #include "worker.h"
    5. #include <QThread>
    6. #include <QTimer>
    7. #include <QPointer>
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QCoreApplication a(argc, argv);
    12.  
    13. QTimer timer;
    14. Worker * worker = new Worker;
    15. // I tried QPointer<Worker> worker = new Worker; also but still gives the same application output
    16.  
    17. QCoreApplication::connect(&timer, SIGNAL(timeout()), worker, SLOT(process()));
    18. QCoreApplication::connect(worker, SIGNAL(stopThread()), &timer, SLOT(stop()));
    19. QCoreApplication::connect(worker, SIGNAL(stopThread()), worker, SLOT(deleteLater()));
    20. timer.start(1000);
    21.  
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //worker.h
    2. #ifndef WORKER_H
    3. #define WORKER_H
    4.  
    5. #include <QObject>
    6.  
    7. class Worker : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. int nCounter;
    12. public:
    13. explicit Worker(QObject *parent = 0);
    14. ~Worker();
    15.  
    16. signals:
    17. void stopThread(void);
    18. public slots:
    19. void process(void);
    20. };
    21.  
    22. #endif // WORKER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // worker.cpp
    2. #include "worker.h"
    3. #include <QDebug>
    4. #include <QTime>
    5. #include <QThread>
    6.  
    7. Worker::Worker(QObject *parent) : QObject(parent),
    8. nCounter(0)
    9. {}
    10.  
    11. Worker::~Worker()
    12. {
    13.  
    14. }
    15.  
    16. void Worker::process(void)
    17. {
    18. nCounter++;
    19. qDebug() << QTime::currentTime().toString("hh:mm:ss");
    20. if (nCounter == 10)
    21. {
    22. qDebug() << "-x-x-x- Count Reached 10 -x-x-x-";
    23. emit stopThread();
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    Kindly help me identify the why the message says the application crashes.
    Qt Code:
    1. Starting /home/rahul/Documents/build-qonsole-Desktop_Qt_5_4_1_GCC_32bit-Debug/qonsole...
    2. /home/rahul/Documents/build-qonsole-Desktop_Qt_5_4_1_GCC_32bit-Debug/qonsole crashed
    To copy to clipboard, switch view to plain text mode 
    Last edited by rawfool; 3rd August 2015 at 17:06.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt application output says crashed

    You'll have to have a look at the stack trace of the crash.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt application output says crashed

    Does the output show "-x-x-x- Count Reached 10 -x-x-x-" if you wait long enough?

    There is nothing wrong with your application, except that:
    • there is a race condition between slots: worker->stopThread() is connected to both timer.stop() and worker->deleteLater(); worker could be deleted first, thus preventing timer.stop() from being run. Since the order in which the slots are run is unspecified in general, this may or may not happen in practice.
    • the application never terminates. Nothing causes the event loop to exit.

    I cannot see why stopping the timer would be necessary. However, you should probably exit the event loop cleanly. Here is a proposal that exits the program after the worker has finished its work:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. QTimer timer;
    5. Worker worker; // No need to allocate this on the heap; the stack is fine, and the object will be automatically destroyed at the end of the function (just like a and timer)
    6. QCoreApplication::connect(&timer, SIGNAL(timeout()), &worker, SLOT(process()));
    7. QCoreApplication::connect(&worker, SIGNAL(stopThread()), &a, SLOT(quit()));
    8. timer.start(1000);
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to yeye_olive for this useful post:

    rawfool (3rd August 2015)

  5. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Qt application output says crashed

    Does the output show "-x-x-x- Count Reached 10 -x-x-x-" if you wait long enough?
    Yes! The output prints the same.

    the application never terminates. Nothing causes the event loop to exit.
    I'm closing the console window. So, doesn't that mean that a signal to terminate the application is emitted implicitly ?

    Your solution worked cleanly though, thank you.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt application output says crashed

    Quote Originally Posted by yeye_olive View Post
    there is a race condition between slots: worker->stopThread() is connected to both timer.stop() and worker->deleteLater(); worker could be deleted first, thus preventing timer.stop() from being run. Since the order in which the slots are run is unspecified in general, this may or may not happen in practice.
    Actually, as of Qt5 the order of slot invocations it now specified as being in order of connects.
    This is de-facto also the case for Qt4.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    rawfool (4th August 2015)

  8. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt application output says crashed

    Quote Originally Posted by rawfool View Post
    I'm closing the console window. So, doesn't that mean that a signal to terminate the application is emitted implicitly ?
    No. It is possible to make this happen, but Qt offers no built-in platform-agnostic way to do it. There are several threads discussing solutions on this forum. That being said, the default behavior (the application is killed without having the opportunity to run clean-up code) is often acceptable.

    Quote Originally Posted by anda_skoa View Post
    Actually, as of Qt5 the order of slot invocations it now specified as being in order of connects. This is de-facto also the case for Qt4.
    Thanks, I must have missed that.

  9. The following user says thank you to yeye_olive for this useful post:

    rawfool (4th August 2015)

Similar Threads

  1. Replies: 1
    Last Post: 17th November 2015, 14:11
  2. prints nothing in application output
    By ramin.lich in forum Newbie
    Replies: 2
    Last Post: 1st October 2014, 09:46
  3. Application output pane
    By stef13013 in forum Newbie
    Replies: 2
    Last Post: 30th June 2013, 18:31
  4. Replies: 3
    Last Post: 27th August 2011, 21:44
  5. Error message: The Application crashed
    By josecarlosmissias in forum Newbie
    Replies: 13
    Last Post: 10th December 2009, 16:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.