April 24, 2011

Polto Polto
Lab Rat
21 posts

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

 Signature 

Polto

7 replies

April 24, 2011

john_god john_god
Lab Rat
267 posts

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.

April 24, 2011

Zlatomir Zlatomir
Hobby Entomologist
366 posts

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

April 24, 2011

Polto Polto
Lab Rat
21 posts

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

 Signature 

Polto

April 24, 2011

vinb vinb
Lab Rat
205 posts

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

April 24, 2011

Polto Polto
Lab Rat
21 posts

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

  1. Widget::~Widget()
  2. {
  3. delete ui;
  4. delete this;
  5. }

 Signature 

Polto

April 24, 2011

Zlatomir Zlatomir
Hobby Entomologist
366 posts

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

April 30, 2011

apollo74 apollo74
Lab Rat
16 posts

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:

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

 
  ‹‹ Printing and total page count in header/footer      Is it a bug with QTextEdit? ››

You must log in to post a reply. Not a member yet? Register here!