Results 1 to 5 of 5

Thread: memory leak in QTableWidget

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question memory leak in QTableWidget

    I have a dialog with a QTableWidget and 2 line edit's.
    The user input on the 2 line edits
    determine the number of rows and columns of QTableWidget.
    Also have a random function called from a popup menu
    that fills the table with random numbers like this:


    Qt Code:
    1. void matrix_dlg::matrixRandom()
    2. {
    3. QString str;
    4. int i;
    5. int l;
    6. int rows = m_ui->tableWidget_matrix1->rowCount();
    7. int columns = m_ui->tableWidget_matrix1->columnCount();
    8.  
    9. for(i=0;i<rows;i++)
    10. {
    11. for(l=0;l<columns;l++)
    12. {
    13. str.sprintf("%d",qrand()/100);
    14.  
    15. if (m_ui->tableWidget_matrix1->item(i,l) == 0)
    16. {
    17. QTableWidgetItem *newItem = new QTableWidgetItem(str);
    18. m_ui->tableWidget_matrix1->setItem(i,l, newItem);
    19. }
    20. else
    21. {
    22. m_ui->tableWidget_matrix1->item(i, l)->setText(str);
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 


    I put a if else to avoid creating new items to the same cell,
    I think this is a correct aproach to avoid memory leak (please correct me if I'm wrong)

    In the destructor, created by Qtcreator, I have

    Qt Code:
    1. matrix_dlg::~matrix_dlg()
    2. {
    3. delete m_ui;
    4. }
    To copy to clipboard, switch view to plain text mode 

    This will be enought to delete the items and free memory or I should add proper code ?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: memory leak in QTableWidget

    Quote Originally Posted by john_god View Post
    I put a if else to avoid creating new items to the same cell,
    I think this is a correct aproach to avoid memory leak (please correct me if I'm wrong)
    It's not directly to avoid memory leak, but it saves time and unnecessary creation of objects etc.
    This will be enought to delete the items and free memory or I should add proper code ?
    It's enough since the table widget deletes all items by itself when it get destroyed.

  3. The following user says thank you to Lykurg for this useful post:

    john_god (23rd June 2009)

  4. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question Re: memory leak in QTableWidget

    It's not directly to avoid memory leak, but it saves time and unnecessary creation of objects etc.
    Can you explain better ? if I alocate twice a QTableWidgetItem to the same cell with new operator, will it create 2 objects and I loose the pointer the the first object, there fore creating a memory leak, or it will ignore the second object ?

    if I try to alocate memory to 2 pointers with same name 'newItem', I will get an error, but it works fine inside the for loop. why?

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: memory leak in QTableWidget

    Quote Originally Posted by john_god View Post
    Can you explain better ? if I alocate twice a QTableWidgetItem to the same cell with new operator, will it create 2 objects and I loose the pointer the the first object, there fore creating a memory leak, or it will ignore the second object ?
    Well, even if YOU loose the pointer, it is still a child of the table view, and this view will take care that it will be destroyed sooner or later. So no memory leak. (You could compare that with the garbage collector of java...)

    if I try to alocate memory to 2 pointers with same name 'newItem', I will get an error, but it works fine inside the for loop. why?
    I guess you have tried something like that:
    Qt Code:
    1. QTableWidgetItem *newItem = new QTableWidgetItem(str);
    2. QTableWidgetItem *newItem = new QTableWidgetItem(str);
    To copy to clipboard, switch view to plain text mode 
    which doesn't work, but this will work:
    Qt Code:
    1. QTableWidgetItem *newItem = new QTableWidgetItem(str);
    2. newItem = new QTableWidgetItem(str);
    To copy to clipboard, switch view to plain text mode 
    Therefor look in any resource of C++ Programming explaining the character of pointers. You only define them once but could assign them multiple. in the scope it works because it is a little world inside where the compiler "forgets" the definition...

  6. The following user says thank you to Lykurg for this useful post:

    john_god (24th June 2009)

  7. #5
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: memory leak in QTableWidget

    Very happy with your help Lykurg . Thank you very much

Similar Threads

  1. Memory leak detection
    By Sid in forum Qt Programming
    Replies: 8
    Last Post: 4th May 2011, 22:38
  2. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  3. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  4. QPixMap and Memory leak
    By Krish_ng in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 14:18
  5. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.