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] Segfault on QApplication exit
Forum Updated to NodeBB v4.3 + New Features

[Solved] Segfault on QApplication exit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 11.1k Views 1 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.
  • G Offline
    G Offline
    Guigui
    wrote on 26 Apr 2013, 19:41 last edited by
    #1

    I've been debugging a segmentation fault that happens when closing a Qt application composed of several projects. We ended up trying this very simple test:

    main.cpp:
    @#include <QApplication>
    #include <QMainWindow>

    int main(int argc, char *argv[])
    {
    // Method 1: CLOSES CORRECTLY
    /QApplication a(argc, argv);
    QMainWindow w;
    w.show();
    return a.exec();
    /

    // Method 2: CAUSES SEGFAULT
    QApplication *a = new QApplication(argc, argv&#41;;
    QMainWindow *w = new QMainWindow();
    w->show();
    return a->exec&#40;&#41;;
    

    }@

    Project file:

    @
    QT += widgets
    TARGET = test
    TEMPLATE = app
    SOURCES += main.cpp@

    I realized that the first method (commented code), always closes correctly and without errors while the second method which uses pointers causes a segfault on exit. This seems to happen randomly. This behavior appeared after migrating from Qt 4.6 to Qt 5.0.1, on CentOS 6.4.

    Anyone would have an idea why the method using pointers does not work correctly anymore? Is this method valid? Is this behavior normal?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on 26 Apr 2013, 20:05 last edited by
      #2

      With your second method, the objects are allocated on the heap, but you never delete them!

      The memory leak that this causes probably doesn't matter much, as the process is going to exit anyway.

      Nonetheless it's bad practice to not clean up your heap objects properly. So please try:

      @main()
      {
      QApplication *a = new QApplication(argc, argv);
      QMainWindow *w = new QMainWindow();

      w->show();
      int ret = a->exec(&#41;;
      
      delete w;
      delete a;
      
      return ret;
      

      }@

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Guigui
        wrote on 26 Apr 2013, 20:16 last edited by
        #3

        Seems like many examples and tutorials I found don't delete objects on the stack declared, probably, as you said, because it doesn't matter in this case. Manually deleting the objects fixed the problem. Still wondering why we didn't notice this before since this is a C/C++ issue and not a Qt one. Anyway, case solved, and I'll stick to good practices. Thanks a lot!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on 26 Apr 2013, 20:18 last edited by
          #4

          Well, you can assume that QApplication and QMainWindow will also allocate system resources, e.g. from the underlying windowing system. If you never destroy these objects, their destructors never get executed and thus they will never get a chance to do a proper clean up, e.g. of system resources. Here things can go wrong!

          BTW: If examples don't delete heap objects, then that's probably because the example code wants to illustrate a specific aspect, but is not intended to be used 1:1 in a real software and thus leaves out various "details".

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mi-La
            wrote on 9 Jan 2018, 14:28 last edited by
            #5

            Though this is marked as solved, I can't see the reason for the SEGFAULT when no desctructors are called. Can anyone explain please?

            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