PDA

View Full Version : crash when delete QTreeWidgetItem*



evgenM
6th December 2006, 13:44
my code:


QList<QTreeWidgetItem*>::iterator itd = itemsToDelete.begin();
while (itd != itemsToDelete.end())
{
QTreeWidgetItem* twi_ = *itd;
if (!twi_->childCount())
{
cout <<twi_<<"-"<<twi_->childCount()<<endl; //log1
cout <<twi_->text(0).toStdString()<<endl; //log2
delete twi_;
twi_ = 0;
//twi_->setText(0, "ChangeText"); //if comment "delete twi_;" this line work
cout <<"ok"<<endl;
itd = itemsToDelete.erase(itd);
cout <<"from List ok"<<endl;
}
else
++itd;
}


now program log:
log1: 0x41617880-0 //normal - no children
log2: itemText // it's right too;

on line "delete twi_;" program crash (debugger mark this line too)
why it can be possible?

P.S my qt version 4.1.4
more precise: crashing only for non topLevelItems

jacek
6th December 2006, 20:46
QList<QTreeWidgetItem*>::iterator itd = itemsToDelete.begin();
...
QTreeWidgetItem* twi_ = *itd;
...
delete twi_;
...
itd = itemsToDelete.erase(itd); // <-- !
*itd points to a deleted object.

evgenM
7th December 2006, 08:55
*itd points to a deleted object.
so what? its not a problem (its just mean (*itd)=0).
btw log from "cout <<"ok"<<endl;" is not appear
and debugger said program crash on "delete twi_;"

jacek
7th December 2006, 16:11
so what? its not a problem
Well... yes, it doesn't matter in this case.


its just mean (*itd)=0.
No, it won't be set to 0.


debugger said program crash on "delete twi_;"
The problem is when you delete an item, you delete also all of its children, so some entries in itemsToDelete might become invalid and deleting a deleted pointer might cause a crash.

evgenM
8th December 2006, 09:26
The problem is when you delete an item, you delete also all of its children, .

if u check my code u will see the line
if (!twi_->childCount())
{
}
its special for this case

jacek
8th December 2006, 12:35
if u check my code u will see the line [...] its special for this case
True, I missed that.

How do you populate itemsToDelete?

evgenM
8th December 2006, 14:29
its not important
my list.count() == 1

i have found - it crash when destructor call twi_->parent();
its bug of inheritance (versions 4.1.1-4.1.4)

with 4.2.2 it work right (no crashing)

but i still have a problem because i need it under 4.1.4

jacek
8th December 2006, 14:42
i have found - it crash when destructor call twi_->parent();
its bug of inheritance (versions 4.1.1-4.1.4)
Is there a task tracker entry for that? I couldn't find anything relevant.


but i still have a problem because i need it under 4.1.4
Maybe you can use takeChild() or takeChildren() before deleting them to avoid the crash? As the last resort, you can always switch to QTreeView and a custom model.

evgenM
8th December 2006, 14:54
if u have a 4.1.4 version
u can try this:

QTreeWidgetItem* parent = new QTreeWidgetItem(treeWidget);
QTreeWidgetItem* child = new QTreeWidgetItem(0);
parent->addChild(child);
/////
now:
parent->child(0) == child;
but
child->parent() == 0;

//if QTreeWidgetItem* child = new QTreeWidgetItem(parent); --- its work right
--------------------------------------------------------------------------------------------

about takeChild();
yes i have trying
child->parent()->takeChild(child->parent()->indexOfChild(child)); :)
same result

jacek
9th December 2006, 21:04
I've created a small test application and it doesn't crash (I use Qt 4.1.5).