Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Catch exceptions in QT/C++ (QGuiApplication) (override notify() method?)
QtWS25 Last Chance

Catch exceptions in QT/C++ (QGuiApplication) (override notify() method?)

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
13 Posts 3 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    komodosp
    wrote on 22 Jun 2022, 22:46 last edited by komodosp
    #1

    How can I override QGuiApplication::notify()

    According to my research this appears to be the way to actually catch exceptions that arise in a QT/C++ application - described in several places, e.g. https://stackoverflow.com/a/14804711/1495940

    However, any description of it or examples I can find speak only of overriding QApplication::notify() or QCoreApplication::notify(), not QGuiApplication::nofity(). (Even though QGuiApplication inherits from QCoreApplication.

    When I try to do it, my (Linux embedded) application crashes when I initialise QQuickView.

    Here is what I have:

    DebugApplication.h

    #ifndef DEBUGAPPLICATION_H
    #define DEBUGAPPLICATION_H
    
    #include <QGuiApplication>
    #include <typeinfo>
    
    class DebugApplication : public QGuiApplication
    {
    public:
        DebugApplication(int argc, char* argv[]);
    
        bool notify(QObject* receiver, QEvent* e) override;
    };
    
    #endif // DEBUGAPPLICATION_H
    

    DebugApplication.cpp:

    #include "debugapplication.h"
    
    DebugApplication::DebugApplication(int argc, char** argv) : QGuiApplication(argc, argv) {
    
    }
    
    bool DebugApplication::notify(QObject * receiver, QEvent* e)
    {
        try {
            return QGuiApplication::notify(receiver, e);
        }
        catch (std::exception & ex) {
            qFatal("Error %s sending event %s to object %s (%s)", ex.what(),
                   typeid(*e).name(),
                   qPrintable(receiver->objectName()),
                   typeid(*receiver).name()
                   );
        }
        catch (...) {
            qFatal("Error <unknown> sending event %s to object %s (%s)",
                   typeid(*e).name(), qPrintable(receiver->objectName()),
                   typeid(*receiver).name());
        }
    
        return false;
    }
    

    And in main.cpp

    qInstallMessageHandler( myMessageOutput );  // method to write messages to file.
    
    DebugApplication app( argc, argv );
    
    // Some initialisation code...
    
    QQuickView view;  // Crashes on this line.
    
    
    view.rootContext()->setContextProperty( QStringLiteral( "contentHandler" ), (QObject *)(new contentLoader( view.rootContext(), &app )) );
    view.setSource( QUrl( QStringLiteral( "qrc:/contentloader.qml" ) ) );
    view.show();
    
    // more stuff
    
    return app.exec();
    

    if I make app a QGuiApplication it works fine. But if I make it a DebugApplication it crashes at the QQuickView *view; line without explanation.

    I will be just as happy with any other method of logging exceptions in the application! Basically my application crashes intermittently with no pattern or in no particular place and I'd like a way of logging info about why and where it crashed, but a regular try... catch just doesn't work, it just dies without falling into the catch.

    It will display (but not log) an error message if I run in debug mode, but in release mode it just says "Process killed by signal" in QTCreator's Application output. Since I have never been able to recreate this crash myself, having an error in Debug mode doesn't help.

    I also have a call to qInstallMessageHandler() which allows me to output qDebug() messages to a file but that doesn't help in this case unless I can catch the crash when it happens to write the output.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 23 Jun 2022, 04:57 last edited by
      #2

      I don't know if this has anything to do with the crash, but your DebugApplication lacks Q_OBJECT macro.

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 23 Jun 2022, 18:58 last edited by
        #3

        Hi and welcome to devnet,

        You constructor signature is wrong.

        argc must be a reference to an int not an int.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply 23 Jun 2022, 22:22
        3
        • S SGaist
          23 Jun 2022, 18:58

          Hi and welcome to devnet,

          You constructor signature is wrong.

          argc must be a reference to an int not an int.

          K Offline
          K Offline
          komodosp
          wrote on 23 Jun 2022, 22:22 last edited by
          #4

          @SGaist

          Thanks! Well that got me past that problem, my application is now running!

          However, overriding the notify() method did nothing... My application still crashes at the exception code without an error message if I run in Release mode.

          In debug mode I get this, which is what I'd expect

          ASSERT failure in QList<T>::operator[]: "index out of range", file /opt/ti/processor-sdk-linux-am335x-evm-04.03.00.05/linux-devkit/sysroots/armv7ahf-neon-linux-gnueabi/usr/include/qt5/QtCore/qlist.h, line 545
          Process killed by signal

          But in release mode just

          Process killed by signal

          Code to cause exception is:

          QStringList stringlist {"hello"};
          qDebug() << "String 2 is " << stringlist[2];
          

          .. and it's in a backend handler for a QML splash screen.

          (Not sure if I should make this another question or not)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 24 Jun 2022, 07:03 last edited by
            #5

            Qt does not raise exceptions, so you cannot catch them.

            (Z(:^

            1 Reply Last reply
            3
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 24 Jun 2022, 07:51 last edited by
              #6

              To add to @sierdzio, you are encountering an assert which is not the same as an exception. In your case, you are not entering a potential runtime error, it's clearly a bug that you have to fix in your code.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • K Offline
                K Offline
                komodosp
                wrote on 24 Jun 2022, 15:12 last edited by
                #7

                Right - thanks for the info guys...

                So it doesn't raise exceptions, but does it do anything when the application crashes that could be used to determine what happened?

                I accept might be a bug in the code, unfortunately at the moment I'm at a loss to find where it is as it seems to happen randomly and rarely, and with seemingly no pattern.

                S 1 Reply Last reply 27 Jun 2022, 08:15
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 24 Jun 2022, 19:36 last edited by
                  #8

                  Do you have a lot of containers in your application ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply 26 Jun 2022, 16:18
                  0
                  • S SGaist
                    24 Jun 2022, 19:36

                    Do you have a lot of containers in your application ?

                    K Offline
                    K Offline
                    komodosp
                    wrote on 26 Jun 2022, 16:18 last edited by
                    #9

                    @SGaist Do you mean like list type objects as described here? https://wiki.qt.io/QML_Containers

                    ...then yeah I do have quite a lot, esp. QList and QVector

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 26 Jun 2022, 18:59 last edited by
                      #10

                      From the crash we know it's one of the QList access that is at fault. Are you doing unchecked access in some specific files ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      K 1 Reply Last reply 26 Jun 2022, 20:58
                      0
                      • S SGaist
                        26 Jun 2022, 18:59

                        From the crash we know it's one of the QList access that is at fault. Are you doing unchecked access in some specific files ?

                        K Offline
                        K Offline
                        komodosp
                        wrote on 26 Jun 2022, 20:58 last edited by
                        #11

                        @SGaist Ahh - maybe I didn't explain myself properly. The Assert failure error message that I put in the message above was actually just a result of my attempt to cause an exception or error message to appear or be logged, so I can have it in place to diagnose the "real" error when it occurs. That could be something different but at the moment the application is just dying.

                        K 1 Reply Last reply 26 Jun 2022, 21:39
                        0
                        • K komodosp
                          26 Jun 2022, 20:58

                          @SGaist Ahh - maybe I didn't explain myself properly. The Assert failure error message that I put in the message above was actually just a result of my attempt to cause an exception or error message to appear or be logged, so I can have it in place to diagnose the "real" error when it occurs. That could be something different but at the moment the application is just dying.

                          K Offline
                          K Offline
                          komodosp
                          wrote on 26 Jun 2022, 21:39 last edited by
                          #12

                          @komodosp
                          Should this code be generating a core dump? Can't even find that....

                          QStringList stringlist {"hello"};
                          qDebug() << "String 2 is " << stringlist[2];
                          
                          1 Reply Last reply
                          0
                          • K komodosp
                            24 Jun 2022, 15:12

                            Right - thanks for the info guys...

                            So it doesn't raise exceptions, but does it do anything when the application crashes that could be used to determine what happened?

                            I accept might be a bug in the code, unfortunately at the moment I'm at a loss to find where it is as it seems to happen randomly and rarely, and with seemingly no pattern.

                            S Offline
                            S Offline
                            sierdzio
                            Moderators
                            wrote on 27 Jun 2022, 08:15 last edited by
                            #13

                            @komodosp said in Catch exceptions in QT/C++ (QGuiApplication) (override notify() method?):

                            So it doesn't raise exceptions, but does it do anything when the application crashes that could be used to determine what happened?

                            No, by itself containers do not attempt to do anything, that's for performance reasons.

                            But you can get all the info you need by using asan (address sanitizer) or running the app under a debugger. Both ways will show you exactly where and why the crash is happening

                            (Z(:^

                            1 Reply Last reply
                            1

                            2/13

                            23 Jun 2022, 04:57

                            topic:navigator.unread, 11
                            • Login

                            • Login or register to search.
                            2 out of 13
                            • First post
                              2/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved