Results 1 to 4 of 4

Thread: can't exit application without various qt errors

  1. #1
    Join Date
    Jul 2017
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default can't exit application without various qt errors

    I have QtCoreApplication running from main.
    the application runs beautifully, at some point I want to invoke shutdown of the application (and process). I tried to use delete later methods but I still get
    QObject was deleted directly. core dumped

    I'm really looking for a quick an dirty solution. I don't care about shutdown sequence, or in what goes in the other threads I'm running. I only want to avoid getting this log error. but the following things I tried got me no where.

    a. when I try to use(from the same thread as the main thread):
    QCoreApplication::instance().quit()
    or
    QTimer::singleShot(0,QCoreApplication::instance(), SLOT(quit()))
    QEventLoop: can't be used without QApplication
    b. If I try exit(0); I get
    "QThread: Destroyed while thread still running"

    what is the minimum requirement qt needs to have as to avoid any possible error at shut down? (and where I can read about it in qt documentation, all I could google up is people facing those kind of mess and do X do Y .. )
    thanks in advance!!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: can't exit application without various qt errors

    So you main looks like this?
    Qt Code:
    1. int main(int argc, char**argv)
    2. {
    3. YourApplicationClass myAppObject; // something like a QMainWindow would be to a GUI app
    4. return app.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 
    and your code is sitting in the exec() loop then you should be able to call:
    Qt Code:
    1. QCoreApplication::exit(0); // it is static
    To copy to clipboard, switch view to plain text mode 
    from anywhere in the main thread, or use a signal connected to QCoreApplication::quit().

    If you are trying to terminate the application before line 5 is reached, then there is no event loop running. That means timers and queued signal/slot connections will not function: possibly the cause of the message in your option (a). If you cannot redesign your main() along usual Qt lines then you exit from this scenario by allowing execution to fall through to the exec() call with a flag set. If flag set then you never call exec().

    Your option (b) is using the library function to summarily terminate the program. Its generally poor form to not clean up your threads, which is why the warning is issued by Qt.

  3. #3
    Join Date
    Jul 2017
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: can't exit application without various qt errors

    hi Chris,
    I really appreciate you answering.. I really want to have a solution and can't seem to progress.
    so yes my main looks like:
    int main(int argc, char**argv)
    {
    QCoreApplication app;
    return app.exec();
    }
    and since the application is up and doing a lot of stuff using signal slot- so the eventLoop is running.
    so there must be some other reason of me getting
    "QEventLoop: can't be used without QApplication"
    what can be the reason for that? where can I read all about that?
    how can it be that the application has ended but its eventLoop still running? Is there any way to debug what is going on there?

    so should "QCoreApplication::instance().quit()" be enough to exit the process with no errors?

    thanks again

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: can't exit application without various qt errors

    If that is all that is in your main(), then your app does nothing. Obviously you are doing something else that you aren't showing us, like trying to create global QObject or QThread instances. Since global variables are initialized -before- main() is executed, you have no event loop running, not even a qApp() instance yet. If you try to do things with global variables that require an event loop or interacting with the qApp() instance, then that's the most obvious reason for your error message.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 8
    Last Post: 29th December 2016, 17:42
  2. How to exit Qt console application?
    By arcelivez in forum Qt Programming
    Replies: 14
    Last Post: 30th July 2014, 08:51
  3. Replies: 4
    Last Post: 19th November 2012, 14:35
  4. [solved]jom exit with code 2 without any errors.
    By pkj in forum Qt Programming
    Replies: 0
    Last Post: 7th September 2012, 08:17
  5. Replies: 2
    Last Post: 21st November 2010, 18:03

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.