Results 1 to 3 of 3

Thread: qFatal is killing my application with msg handler installed.

  1. #1
    Join Date
    Oct 2011
    Location
    Australia
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default qFatal is killing my application with msg handler installed.

    Hi

    I realize the default behavior of qFatal() is to terminate an application. However the documentation suggests if I install a message handler it wont. But it is. How can I stop qFatal() from killing my application?

    To clarify I have the following code. I actually do want to quit on qFatal() but I want to do clean up too. In this code the qApp->quit() slot is never executed because its asyncronous and something is killing my app before the event loop has a chance to pick it up. (omitting the arbitrary bit of code somewhere that actually calls a `qFatal()` because its not important):

    Qt Code:
    1. #include <QtGlobal>
    2. #include <QApplication>
    3. #include <unistd.h>
    4. #include <stdio.h>
    5.  
    6. void LogMsgHandler(QtMsgType type, const char *msg)
    7. {
    8. switch (type) {
    9. #ifndef QT_NO_DEBUG_OUTPUT
    10. case QtDebugMsg:
    11. printf("DEBUG: %s\n", msg);
    12. break;
    13. #endif
    14. case QtWarningMsg:
    15. printf("WARNING: %s\n", msg);
    16. break;
    17. case QtCriticalMsg:
    18. printf("CRITICAL: %s\n", msg);
    19. break;
    20. case QtFatalMsg:
    21. printf("FATAL: %s\n", msg);
    22. qApp->quit(); // Does not generate aboutToQuit(). Does not exit event loop.
    23. }
    24. printf("Exiting handler\n");
    25. }
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. qInstallMsgHandler(LogMsgHandler);
    30. QCoreApplication app(argc, argv);
    31. Controller controller;
    32. app.exec();
    33. printf("Never get here. Because something kill my app.\n");
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thanks.
    Last edited by 33333; 5th July 2014 at 03:24.

  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: qFatal is killing my application with msg handler installed.

    qFatal() calls qt_message_output() internally which, after calling your handler, terminates the program using the _CrtDbgBreak(), abort(), or exit(1) call (depends on platform). The message handler allows you to do something custom with the message, not suppress the termination. You could just call your clean up code before returning from your message handler for fatal messages (or warnings where QT_FATAL_WARNINGS is set). Alternatively,
    • You can install a signal handler for SIGABRT to trap the abort() call.
    • You can use atexit() or something similar to register a function to run after exit() is called.
    • I have no idea how you intercept the Windows function.

  3. The following user says thank you to ChrisW67 for this useful post:

    33333 (5th July 2014)

  4. #3
    Join Date
    Oct 2011
    Location
    Australia
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: qFatal is killing my application with msg handler installed.

    Ah OK. It aborts on Linux. The docs didn't quite say overriding the msg handler would override the abort behavior though the wording suggested it. Suks a bit. The whole motivation for me was reusing the q(Debug|Message|Critical|Fatal) function and semantics - except for the abort(). They're kind of limiting anyway, so maybe I'll just write my own layer, use that in my own code exclusively, and just use the message handler to pipe stuff from third party libs that use these appropriately.

    On catching SIGABRT; I really wanted to use aboutToQuit() to decouple the cleanup() from the signal handler (if its a safe signal to return from). `abort()` never returns, so if Qt actually uses abort() the event loop won't be reentered. I'll have to investigate .

    Thanks.

Similar Threads

  1. Replies: 4
    Last Post: 27th October 2013, 05:25
  2. Replies: 6
    Last Post: 14th October 2010, 13:19
  3. Replies: 1
    Last Post: 13th November 2009, 18:40
  4. qFatal question
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2007, 14:51
  5. cleanup handler on application close
    By err23 in forum Qt Programming
    Replies: 3
    Last Post: 16th August 2006, 10:56

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.