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. Qdialog causing memory leak?

Qdialog causing memory leak?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 4.5k 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.
  • liewjlsL Offline
    liewjlsL Offline
    liewjls
    wrote on last edited by
    #1

    Hi,

    I have a program which will need to create few qdialog depends on the user. My QDialog contains quite a few widgets, buttons and textline, they are created using Qt Designer UI. which all the buttons, textline and widgets etc are being created in my .UI file. I think i have some memory leak whenever i have create and close the dialog, I tried run valgrind, it didn't complains about my qdialog but when "top" commands shows the memory percentage doesn't go down after i create and close dialog.

    I have set my qdialog with WA_DeleteOnClose in my QDialog constructor as few ppl suggested.

    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        setAttribute(Qt::WA_DeleteOnClose, true)
    }
    
    Dialog::~Dialog()
    {
        qDebug() << "Dialog object is being deleted!";
        delete ui;
    }
    
    

    i used deleteLater() to delete my Dialog. Will this cause any memory leak? Does this will automatically delete all my widgets, buttons and textline?

        Dialog * dialogObj = new Dialog;
        dialogObj->deleteLater();
    
    

    Thanks.

    jsulmJ A 2 Replies Last reply
    0
    • liewjlsL liewjls

      Hi,

      I have a program which will need to create few qdialog depends on the user. My QDialog contains quite a few widgets, buttons and textline, they are created using Qt Designer UI. which all the buttons, textline and widgets etc are being created in my .UI file. I think i have some memory leak whenever i have create and close the dialog, I tried run valgrind, it didn't complains about my qdialog but when "top" commands shows the memory percentage doesn't go down after i create and close dialog.

      I have set my qdialog with WA_DeleteOnClose in my QDialog constructor as few ppl suggested.

      
      Dialog::Dialog(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Dialog)
      {
          ui->setupUi(this);
          setAttribute(Qt::WA_DeleteOnClose, true)
      }
      
      Dialog::~Dialog()
      {
          qDebug() << "Dialog object is being deleted!";
          delete ui;
      }
      
      

      i used deleteLater() to delete my Dialog. Will this cause any memory leak? Does this will automatically delete all my widgets, buttons and textline?

          Dialog * dialogObj = new Dialog;
          dialogObj->deleteLater();
      
      

      Thanks.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @liewjls Why not just

      delete dialogObj;
      

      as soon as you do not need the instance any-more?
      Deleting the dialog deletes all its widgets.
      Can you show how you are using the dialog?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Suares
        wrote on last edited by Suares
        #3

        said in Qdialog causing memory leak?:

        when "top" commands shows the memory percentage doesn't go down after i create and close dialog

        It doesn't mean that you have a memory leak.

        1 Reply Last reply
        1
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Using Qt::WA_DeleteOnClose in a constructor is a bad bad idea. If someone creates an instance of that class on a stack you've got a crash. Same thing if you allocate on a heap and try to delete it after it was closed.

          Deleting an object doesn't mean that the OS will instantly free all resources associated with a process. Memory managers are a large scale garbage collectors. As @Suares said it doesn't mean you've got a memory leak. What matters is if the amount of memory you free is the same as you allocated.

          1 Reply Last reply
          4
          • liewjlsL liewjls

            Hi,

            I have a program which will need to create few qdialog depends on the user. My QDialog contains quite a few widgets, buttons and textline, they are created using Qt Designer UI. which all the buttons, textline and widgets etc are being created in my .UI file. I think i have some memory leak whenever i have create and close the dialog, I tried run valgrind, it didn't complains about my qdialog but when "top" commands shows the memory percentage doesn't go down after i create and close dialog.

            I have set my qdialog with WA_DeleteOnClose in my QDialog constructor as few ppl suggested.

            
            Dialog::Dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Dialog)
            {
                ui->setupUi(this);
                setAttribute(Qt::WA_DeleteOnClose, true)
            }
            
            Dialog::~Dialog()
            {
                qDebug() << "Dialog object is being deleted!";
                delete ui;
            }
            
            

            i used deleteLater() to delete my Dialog. Will this cause any memory leak? Does this will automatically delete all my widgets, buttons and textline?

                Dialog * dialogObj = new Dialog;
                dialogObj->deleteLater();
            
            

            Thanks.

            A Offline
            A Offline
            ambershark
            wrote on last edited by
            #5

            @liewjls Like @Suares said, don't watch top for memory leaks. It doesn't work like that.

            Read up on how the OS allocates and frees memory pages to better understand why your memory in use doesn't go down after you free something. Especially something as small as a QDialog.

            If valgrind is not reporting a leak, then you don't have a leak. :)

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            0
            • liewjlsL Offline
              liewjlsL Offline
              liewjls
              wrote on last edited by
              #6

              Thanks.
              I did use valgrind to check on the memory on my development machine. But! I got a problem, some of the codes need to run on a PC which the PC is running on OpenSuse 11.3 and I couldn't run valgrind to check everything is working OK.

              @Chris-Kawa oh, where should I use Qt::WA_DeleteOnClose then?

              thanks.

              Chris KawaC jsulmJ 2 Replies Last reply
              0
              • nestoracN Offline
                nestoracN Offline
                nestorac
                wrote on last edited by
                #7

                Memory is probably reserved for the cache. You should try "free -m" to see the overall values. But I agree, it's not a memory leak. It's just the OS creating objects and reserving then the memory left when those objects get destroyed for other purposes. Nothing new. You can see how the memory gets more used as time passes in Linux. This is about the kernel itself.

                1 Reply Last reply
                0
                • liewjlsL liewjls

                  Thanks.
                  I did use valgrind to check on the memory on my development machine. But! I got a problem, some of the codes need to run on a PC which the PC is running on OpenSuse 11.3 and I couldn't run valgrind to check everything is working OK.

                  @Chris-Kawa oh, where should I use Qt::WA_DeleteOnClose then?

                  thanks.

                  Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @liewjls said in Qdialog causing memory leak?:

                  oh, where should I use Qt::WA_DeleteOnClose then?

                  This flag should be passed by the user of your class, where the information about the lifetime of the object is. This is usually at the place the instance of that class is created.
                  For example:

                  void SomeClass::showDialog() {
                      Dialog* dialogObj = new Dialog(this); //parents are important!
                      dialogObj->setAttribute(Qt::WA_DeleteOnClose);
                      dialogObj->show();
                  }
                  

                  Here you know what the lifetime is i.e. it is created on the heap so it won't be deleted automatically when going out of scope. If the parent is deleted it will also delete the dialog. If the user closes the dialog it will also be deleted via the flag. The user of the Dialog class (i.e. SomeClass instance) fully governs the lifetime of the instance it creates and doesn't need to rely on internal implementation of the Dialog class.

                  Parents of dialogs are important (as I noted in the comment). They keep the proper stacking order of the windows and assure proper focus switching handling. It's also easier to manage lifetime of such dialog because it is assured that there's no leak (parent always deletes its child). You can always call deleteLater() earlier if you need to, but there's no danger that some code path will not call it and thus leak.

                  1 Reply Last reply
                  1
                  • liewjlsL liewjls

                    Thanks.
                    I did use valgrind to check on the memory on my development machine. But! I got a problem, some of the codes need to run on a PC which the PC is running on OpenSuse 11.3 and I couldn't run valgrind to check everything is working OK.

                    @Chris-Kawa oh, where should I use Qt::WA_DeleteOnClose then?

                    thanks.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @liewjls Why couldn't you use Valgrind on OpenSuse?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    liewjlsL 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @liewjls Why couldn't you use Valgrind on OpenSuse?

                      liewjlsL Offline
                      liewjlsL Offline
                      liewjls
                      wrote on last edited by
                      #10

                      Hi @jsulm

                      Not sure why thought. When i tried to run valgrind on the machine, i got the error message

                      ==4849== Command: ./test--platform xcb
                      ==4849== 
                      --4849-- Warning: DWARF2 CFI reader: unhandled DW_OP_ opcode 0x2a
                      
                      valgrind: m_debuginfo/readdwarf.c:2199 (copy_convert_CfiExpr_tree): Assertion 'srcix >= 0 && srcix < VG_(sizeXA)(srcxa)' failed.
                      ==4849==    at 0x380273D0: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
                      
                      sched status:
                        running_tid=0
                      

                      I only download and install the valgrind and debuginfo rpm from the opensuse respo.
                      Anyone can help me on that?

                      thanks.

                      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