Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTableWidget memory leak or not?

Suppose I have a 2D array full of data say 10 x 10. The contents, as well as a number of rows, can change any time. Now I want to display this data in a QTableWidget.
I use a timer with time out 1sec to refresh the table contents. In the timeout slot if I use

void slot_timeOut()
{
    //Iterate over the rows
    //and for each cell do something like
    ui->tw_data->setItem(row, 0, new TableWidgetItem(data[row][0]);
    ui->tw_data->setItem(row, 0, new TableWidgetItem(data[row][1]);
    //...
    ui->tw_data->setItem(row, 0, new TableWidgetItem(data[row][9]);
}

the use out new TableWidgetItem worries me. I have no reference to it and I never delete it.
Over a period of time is this a memory leak, or is this managed by Qt, pls help...

like image 367
Anjanu Avatar asked Feb 24 '14 19:02

Anjanu


3 Answers

There is no leak, as

The table takes ownership of the item.

(From QTableWidget::setItem()).

Ownership here means that the QTableWidget will take care of deleting the item when its not longer needed, or the QTableWidget itself is destroyed. Ownership is usually documented in the Qt documentation (if not, I'd consider that a Qt bug).

Once setItem() returns from being called on the same cell, the previously set QTableWidgetItem will be deleted:

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    QTableWidget widget(2, 1);
    QTableWidgetItem* foo = new QTableWidgetItem("Foo");
    widget.setItem(0, 0, foo);
    qDebug() << foo->text(); //works
    widget.setItem(0, 0, new QTableWidgetItem("Bar")); //replaces foo with bar and deletes foo
    qDebug() << foo->text(); // Undefined (usually, crash)
    widget.show();
    return app.exec();
}

If you're on Linux, you can also verify the behavior by running above code (without the second qDebug()) in valgrind with --leak-check=full.

like image 80
Frank Osterfeld Avatar answered Nov 19 '22 06:11

Frank Osterfeld


From the Qt documentation:

void QTableWidget::setItem ( int row, int column, QTableWidgetItem * item ) Sets the item for the given row and column to item. The table takes ownership of the item.

This indicates that Qt will manage the memory for the object as necessary.

like image 27
Chris Avatar answered Nov 19 '22 06:11

Chris


It will delete properly when the you add Q-objects to the cells. I was pushing thousands of C-strings in cells and watched my memory blow up. Made my "raw text data" a QString and my problem was solved.

like image 2
31337newbie Avatar answered Nov 19 '22 08:11

31337newbie