PDA

View Full Version : memory leak in QTableWidget



john_god
23rd June 2009, 02:38
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:



void matrix_dlg::matrixRandom()
{
QString str;
int i;
int l;
int rows = m_ui->tableWidget_matrix1->rowCount();
int columns = m_ui->tableWidget_matrix1->columnCount();

for(i=0;i<rows;i++)
{
for(l=0;l<columns;l++)
{
str.sprintf("%d",qrand()/100);

if (m_ui->tableWidget_matrix1->item(i,l) == 0)
{
QTableWidgetItem *newItem = new QTableWidgetItem(str);
m_ui->tableWidget_matrix1->setItem(i,l, newItem);
}
else
{
m_ui->tableWidget_matrix1->item(i, l)->setText(str);
}
}
}
}


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


matrix_dlg::~matrix_dlg()
{
delete m_ui;
}

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

Lykurg
23rd June 2009, 08:15
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.

john_god
23rd June 2009, 21:03
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?

Lykurg
23rd June 2009, 21:24
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:

QTableWidgetItem *newItem = new QTableWidgetItem(str);
QTableWidgetItem *newItem = new QTableWidgetItem(str);

which doesn't work, but this will work:

QTableWidgetItem *newItem = new QTableWidgetItem(str);
newItem = new QTableWidgetItem(str);
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...

john_god
24th June 2009, 03:13
Very happy with your help Lykurg :D. Thank you very much :cool: