PDA

View Full Version : QTreewidget clear creates memory leaks



moh.gup@gmail.com
1st April 2011, 07:25
Hi,
I am using Qtreewideget.I have to add Qtreewidgetitem in tree widget at runtime.After a period I clear tree wideget by using clear() funtion (also try using delete explictly).
But its seems memory doesnt released.Its kept on increasing.
Since my process is periodic.Applcation hang after some time

Please suggest

stampede
1st April 2011, 07:55
There are lot of people using QTreeWidgetItems / QTreeWidget without problems ( including me ), so this is probably problem with your code. Show the code, maybe someone will help you.

moh.gup@gmail.com
1st April 2011, 09:43
Hi ,
Please check given function.Its call after 3 sec periodically.
Please let me know where i am wrong


void MainWindow::parseMainscreen(QString mainstring)
{
if(mainstring!="")
{
QStringList arrVisDetails = QString(mainstring).split("<fld>",QString::KeepEmptyParts,Qt::CaseInsensitive);
if(arrVisDetails.count!=0)
ui->Monitor_tree->clear();

for(int i=0; i<arrVisDetails .count(); i++)
{
qtreewidgetitem1= new QTreeWidgetItem();
qtreewidgetitem1->setText(0,arrVisDetails .at(i));
ui->Monitor_tree->addTopLevelItem(qtreewidgetitem1);
}

}

}


Thanks

stampede
1st April 2011, 10:24
So if you delete items manually like this:


void MainWindow::ParseMainscreen(QString mainstring){
if(mainstring!=""){
QStringList arrVisDetails = QString(mainstring).split("<fld>",QString::KeepEmptyParts,Qt::CaseInsensitive);
const int count = arrVisDetails.count();
if( count ){
for( int i=ui->Monitor_tree->topLevelItemCount()-1; i>=0 ; --i ){
delete ui->Monitor_tree->takeTopLevelItem(i);
}
for( int i=0 ; i<count ; ++i ){
QTreeWidgetItem * item = new QTreeWidgetItem();
item->setText(0,arrVisDetails .at(i));
ui->Monitor_tree->addTopLevelItem(item);
}
}
}
}

memory still leaks ? This method looks ok (I assume that arrVisDetails.count!=0 is a typo), are you sure you are not doing something else in your code ?
Try to separate this method into another small app, this way you can see if its this method that leaks.

high_flyer
1st April 2011, 10:38
Why do you think its your tree that is leaking?
Don't you allocate anything else on the heap?

moh.gup@gmail.com
1st April 2011, 18:42
Thamsks,
I will try it

wysota
1st April 2011, 19:00
clear() deletes all the items in the widget, there is no leak here.