VireX
2nd May 2007, 23:06
I know it deletes automatically any QObjects but for some reason the delete of QTreeWidgetItems causes problems.
What happens with the following code is, I use SafeDeleteItem to delete a QTreeWidgetItem or a USER from the list. However, sometimes when users are deleted from the list more and more of them begin to exist (because we dont delete them live, if we do use delete pItem it will CRASH the program).
I mean pretend I click CreateOwnerGroup button, and suddenly a group is created, and then pretend I click DeleteOwnerGroup Button... Well now the group I just made is GONE. However, since we didn't use delete... If I press Create Delete Create Delete Create Delete like 50 times, i'll have like 50 MBs of more memory in the processlist because the TreeWidgets are never deleted, they just disappear, but more and more memory adds up, and pretty soon if a user abuses this they can crash their comp because of too much memory use (its a memory leak).
Can anyone help me properly delete?
I do not understand why Qt doesn't have a simple function like this:
QTreeWidget->deleteRow(QTreeWidgetItem*);
Example is like you would delete using the following code:
static void SafeDeleteItem( QTreeWidgetItem *pItem )
{
QTreeWidgetItem *pParent = pItem->parent();
assert( pParent );
int idx = pParent->indexOfChild( pItem );
pParent->takeChild( idx );
//delete pItem; // mem leak WTF! FIXME
}
void Cclass::DeleteUser( int id, bool wasOwner)
{
QString qsDisplayName = m_pMainClass->GetUserInfo(id).DisplayName();
QString qsChatMsg(QString("<span style=\"color: #858585;\"><%1 has been deleted from list></span>").arg(qsDisplayName));
echoLog(qsChatMsg);
if(wasOwner){ // if the user was the owner of the list, then move the users up
// delete MapItems[id]; // delete first from the std::map!!!
SafeDeleteItem(MapItems[id]);
//MapItems.erase(id); // remove old item
LOG("a");
// delete MapItems[GetOwner()]; // remove old item for the new host
SafeDeleteItem(MapItems[GetOwner()]);
LOG("b");
MapItems[GetOwner()] = new QTreeWidgetItem(OwnerTopLevelNode); // spawn new item for the new Owner
MapItems[GetOwner()]->setText(0, m_pMainClass->GetUserInfo(GetOwner()).DisplayName());
LOG("c");
}
else
{
// If they weren't owner just delete their item
// delete MapItems[id];
SafeDeleteItem(MapItems[id]);
MapItems.erase(id);
}
}
Basically this code
What happens with the following code is, I use SafeDeleteItem to delete a QTreeWidgetItem or a USER from the list. However, sometimes when users are deleted from the list more and more of them begin to exist (because we dont delete them live, if we do use delete pItem it will CRASH the program).
I mean pretend I click CreateOwnerGroup button, and suddenly a group is created, and then pretend I click DeleteOwnerGroup Button... Well now the group I just made is GONE. However, since we didn't use delete... If I press Create Delete Create Delete Create Delete like 50 times, i'll have like 50 MBs of more memory in the processlist because the TreeWidgets are never deleted, they just disappear, but more and more memory adds up, and pretty soon if a user abuses this they can crash their comp because of too much memory use (its a memory leak).
Can anyone help me properly delete?
I do not understand why Qt doesn't have a simple function like this:
QTreeWidget->deleteRow(QTreeWidgetItem*);
Example is like you would delete using the following code:
static void SafeDeleteItem( QTreeWidgetItem *pItem )
{
QTreeWidgetItem *pParent = pItem->parent();
assert( pParent );
int idx = pParent->indexOfChild( pItem );
pParent->takeChild( idx );
//delete pItem; // mem leak WTF! FIXME
}
void Cclass::DeleteUser( int id, bool wasOwner)
{
QString qsDisplayName = m_pMainClass->GetUserInfo(id).DisplayName();
QString qsChatMsg(QString("<span style=\"color: #858585;\"><%1 has been deleted from list></span>").arg(qsDisplayName));
echoLog(qsChatMsg);
if(wasOwner){ // if the user was the owner of the list, then move the users up
// delete MapItems[id]; // delete first from the std::map!!!
SafeDeleteItem(MapItems[id]);
//MapItems.erase(id); // remove old item
LOG("a");
// delete MapItems[GetOwner()]; // remove old item for the new host
SafeDeleteItem(MapItems[GetOwner()]);
LOG("b");
MapItems[GetOwner()] = new QTreeWidgetItem(OwnerTopLevelNode); // spawn new item for the new Owner
MapItems[GetOwner()]->setText(0, m_pMainClass->GetUserInfo(GetOwner()).DisplayName());
LOG("c");
}
else
{
// If they weren't owner just delete their item
// delete MapItems[id];
SafeDeleteItem(MapItems[id]);
MapItems.erase(id);
}
}
Basically this code