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]why memory leak in qwidget or qdialog?
Forum Updated to NodeBB v4.3 + New Features

[Solved]why memory leak in qwidget or qdialog?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 9.7k 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.
  • P Offline
    P Offline
    Polto
    wrote on last edited by
    #1

    The problem is :
    I made a empty widget just have a button
    when click this button i open new empty qdialog
    that is so easy
    by monitoring the memory :
    when I open new empty qdialog ,the memory increased by about 500kb
    and when i close the qdialog , the memory didn't lose this 500kb
    and echa time i click the button to open new qdialog the memory increased
    I think something is not right!!!

    Polto

    1 Reply Last reply
    0
    • J Offline
      J Offline
      john_god
      wrote on last edited by
      #2

      You have to set the atribute of the widget with the function
      void QWidget::setAttribute ( Qt::WidgetAttribute attribute, bool on = true )
      and with the flag Qt::WA_DeleteOnClose
      You can use this function on the constructor.

      1 Reply Last reply
      0
      • ZlatomirZ Offline
        ZlatomirZ Offline
        Zlatomir
        wrote on last edited by
        #3

        My guess is that you create a new QDialog object each time the slot executes.

        An alternate way is to create the object once, and in the slot you can check if the object is created or not (use the QDialog* as a member and initialize it to 0 in the mainwindow constructor) and if the pointer == 0 create an object, initialize the pointer and show the QDialog, else just show the dialog.

        //i didn't use this, but it should work, deleteOnClose attribute is off by default so closing the dialog doesn't delete the object (so the pointer should remain valid) - more experienced guys please correct me if i'm wrong

        LE: too late

        https://forum.qt.io/category/41/romanian

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Polto
          wrote on last edited by
          #4

          thanks it really solved what i ask
          but i found other problem it is in opencv iplimage where you can use delete keyword to delete it from memory opencv come with function cvReleaseImage to delelet it but it doesn't work in qt

          Polto

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vinb
            wrote on last edited by
            #5

            delete is also a normal keyword in Qt.
            try "delete this" where this can be any object.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Polto
              wrote on last edited by
              #6

              setAttribute ( Qt::WA_DeleteOnClose, true );
              this is delete the QWidget from memory

              but when i use delete this; in the Destructor function it do nothing
              @Widget::~Widget()
              {
              delete ui;
              delete this;
              }
              @

              Polto

              1 Reply Last reply
              0
              • ZlatomirZ Offline
                ZlatomirZ Offline
                Zlatomir
                wrote on last edited by
                #7

                You shouldn't use delete this; (not even in other places/reasons) because the object might be created on the stack and you can't "delete" it.

                Also careful with WA_DeleteOnClose, don't use it for widgets created on stack.

                I'm pretty sure that when vinb wrote this he didn't meant the this pointer of your class, he meant that delete still works (Qt is not a separate language is still C++).

                https://forum.qt.io/category/41/romanian

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  apollo74
                  wrote on last edited by
                  #8

                  Hi guys,

                  I'm working under Linux and using Qt Creator 2.01 (Qt 4.7, 32bit). I created a new project (Qt C++ GUI application), added nothing, compiled and run it. Of course the only thing that comes out is a dialog window. I use Valgrind to check for memory leaks and I get:

                  @
                  ==3959== Memcheck, a memory error detector
                  ==3959== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
                  ==3959== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
                  ==3959== Command: ./qt2Basic
                  ==3959==
                  ==3959==
                  ==3959== HEAP SUMMARY:
                  ==3959== in use at exit: 1,093,449 bytes in 10,103 blocks
                  ==3959== total heap usage: 50,353 allocs, 40,250 frees, 5,568,548 bytes allocated
                  ==3959==
                  ==3959== LEAK SUMMARY:
                  ==3959== definitely lost: 2,344 bytes in 9 blocks
                  ==3959== indirectly lost: 7,160 bytes in 358 blocks
                  ==3959== possibly lost: 612,781 bytes in 4,332 blocks
                  ==3959== still reachable: 471,164 bytes in 5,404 blocks
                  ==3959== suppressed: 0 bytes in 0 blocks
                  ==3959== Rerun with --leak-check=full to see details of leaked memory
                  ==3959==
                  ==3959== For counts of detected and suppressed errors, rerun with: -v
                  ==3959== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 208 from 13)
                  @

                  So from what I understand, I'm getting 1MB lost after execution, right? How can I clean up all the memory thoroughly?

                  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