Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Application sometimes(!) crashes
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Application sometimes(!) crashes

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.3k Views 2 Watching
  • 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.
  • W Offline
    W Offline
    Wurgl
    wrote on last edited by Wurgl
    #1

    I have here a rather "complex" example:

    #include <QtWidgets/qapplication.h>
    #include <QtWidgets/qdialog.h>
    
    class MyMainWindow : public QDialog
    {
    public:
        MyMainWindow();
        virtual ~MyMainWindow();
    };
    
    MyMainWindow::MyMainWindow()
        : QDialog()
    {
        setWindowTitle(tr("MyMainWindow - Crash"));
    }
    
    MyMainWindow::~MyMainWindow()
    {
    }
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    
        MyMainWindow *theWindow = new MyMainWindow();
        theWindow->show();
        app.exec();
    
        exit(0);
    }
    

    compiled on OpenSuse 13.2 with
    g++ -c -g -fPIC -Wall -W -Wextra -I /usr/include/qt5 main.cpp -o gmain.o
    g++ gmain.o -g -L/usr/lib64 -lQt5Gui -lQt5Widgets -lQt5Core -o gcrash

    The only thing I do, is starting that program and press ESC to stop it. After doing this a few times, the program dumps core.

    $ ./gcrash
    $ ./gcrash
    $ ./gcrash
    $ ./gcrash
    $ ./gcrash
    Segmentation fault (core dumped)
    $ ./gcrash
    $ ./gcrash
    $ ./gcrash
    $ ./gcrash
    $ ./gcrash
    Segmentation fault (core dumped)

    This is all, what gdb tells me.
    (gdb) where
    #0 0x00007fcdf50aebd9 in ()
    #1 0x0000000000000000 in ()
    (gdb) disassemble 0x00007fcdf50aebd9
    No function contains specified address.

    I have no idea how to fix this. I have even no idea how this strange address is reached, it seems that it happens in some cleanup-code in exit() or similar.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Misty River
      wrote on last edited by
      #2

      Segmentation fault is when you try to access memory that you're not allowed or outside userspace.. my best guest is that you have a defective pointer in your myWindow class.. if it's not in one of the function you're probably trying to delete a member that's already been deleted by Qt hierarchy mechanism.. if you really can't find in your code compile with debug symbols and run it in gdb.. if memory serves me right you type backtrace in gdb to get call stack and then you can move between frames.. with symbols you see immediately where the crash occurs... post your myWindow code....

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Wurgl
        wrote on last edited by
        #3

        Since I can reduce the code to reproduce the crash even more to this size:

        #include <QtWidgets/qapplication.h>
        #include <QtWidgets/qdialog.h>
        
        int main(int argc, char **argv)
        {
            QApplication app(argc, argv);
        
            app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
        
            QDialog *theWindow = new QDialog();
            theWindow->show();
            app.exec();
        
            exit(0);
        }
        

        It is so simple, but shows the same crashes

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wurgl
          wrote on last edited by
          #4

          When I add "sleep(1);" just before the call of "exit(0);", then I see no more crash.

          "ps" shows me, that there are two threads running. So it smells like a problem with synchronization of those threads.

          BTW: valgrind does not show any problem. But valgrind slows down the program, so it may have the same effect like the call to "sleep()".

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rondog
            wrote on last edited by
            #5

            I don't know if this is related but I have a couple of observations. Q_OBJECT is missing and some odd things in main().

            class MyMainWindow : public QDialog
            {
            Q_OBJECT
            public:
            ...
            int main(int argc, char **argv)
            {
                QApplication app(argc, argv);
            
                // app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
            
                MyMainWindow *theWindow = new MyMainWindow();
                theWindow->show();
                return app.exec();
            
               // exit(0);
            }
            1 Reply Last reply
            0
            • W Offline
              W Offline
              Wurgl
              wrote on last edited by
              #6

              exit() vs. return seems to cause the problem.

              The only difference I see, is that exit() does not delete the QApplication object, wheras a return deletes it.

              Hmm … changing app to a pointer and delete that pointer before the call to exit works too.

              Strange! Very strange! All that exit() worked fine with qt4, so I did not even think that this could be a problem.

              However, thanks!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Technically the last line should be something like return app.exec();

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

                W 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Technically the last line should be something like return app.exec();

                  W Offline
                  W Offline
                  Wurgl
                  wrote on last edited by
                  #8

                  @SGaist

                  I disagree. I do this, because I decide which exitcode the application returns to the shell.

                  A code sniplet like
                  if(app.exec())
                  return 42;
                  might be correct as any other value.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Sorry, my message was incomplete. I wanted to say that with calling exit you were probably hitting the data races part from exit.

                    You're indeed free to return whatever value you want after exec has returned.

                    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
                    0

                    • Login

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