[Solved]why memory leak in qwidget or qdialog?



  • 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!!!



  • 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.



  • 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



  • 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



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



  • 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;
    }
    @



  • 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++).



  • 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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.