Results 1 to 10 of 10

Thread: crash when delete QTreeWidgetItem*

  1. #1
    Join Date
    Mar 2006
    Posts
    48
    Thanks
    5
    Thanked 4 Times in 3 Posts

    Post crash when delete QTreeWidgetItem*

    my code:
    Qt Code:
    1. QList<QTreeWidgetItem*>::iterator itd = itemsToDelete.begin();
    2. while (itd != itemsToDelete.end())
    3. {
    4. QTreeWidgetItem* twi_ = *itd;
    5. if (!twi_->childCount())
    6. {
    7. cout <<twi_<<"-"<<twi_->childCount()<<endl; //log1
    8. cout <<twi_->text(0).toStdString()<<endl; //log2
    9. delete twi_;
    10. twi_ = 0;
    11. //twi_->setText(0, "ChangeText"); //if comment "delete twi_;" this line work
    12. cout <<"ok"<<endl;
    13. itd = itemsToDelete.erase(itd);
    14. cout <<"from List ok"<<endl;
    15. }
    16. else
    17. ++itd;
    18. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by evgenM; 6th December 2006 at 13:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: crash when delete QTreeWidgetItem*

    Quote Originally Posted by evgenM View Post
    QList<QTreeWidgetItem*>::iterator itd = itemsToDelete.begin();
    ...
    QTreeWidgetItem* twi_ = *itd;
    ...
    delete twi_;
    ...
    itd = itemsToDelete.erase(itd); // <-- !
    *itd points to a deleted object.

  3. #3
    Join Date
    Mar 2006
    Posts
    48
    Thanks
    5
    Thanked 4 Times in 3 Posts

    Default Re: crash when delete QTreeWidgetItem*

    Quote Originally Posted by jacek View Post
    *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_;"
    Last edited by evgenM; 7th December 2006 at 08:03.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: crash when delete QTreeWidgetItem*

    Quote Originally Posted by evgenM View Post
    so what? its not a problem
    Well... yes, it doesn't matter in this case.

    Quote Originally Posted by evgenM View Post
    its just mean (*itd)=0.
    No, it won't be set to 0.

    Quote Originally Posted by evgenM View Post
    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.

  5. #5
    Join Date
    Mar 2006
    Posts
    48
    Thanks
    5
    Thanked 4 Times in 3 Posts

    Default Re: crash when delete QTreeWidgetItem*

    Quote Originally Posted by jacek View Post
    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: crash when delete QTreeWidgetItem*

    Quote Originally Posted by evgenM View Post
    if u check my code u will see the line [...] its special for this case
    True, I missed that.

    How do you populate itemsToDelete?

  7. #7
    Join Date
    Mar 2006
    Posts
    48
    Thanks
    5
    Thanked 4 Times in 3 Posts

    Default Re: crash when delete QTreeWidgetItem*

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: crash when delete QTreeWidgetItem*

    Quote Originally Posted by evgenM View Post
    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.

    Quote Originally Posted by evgenM View Post
    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.

  9. #9
    Join Date
    Mar 2006
    Posts
    48
    Thanks
    5
    Thanked 4 Times in 3 Posts

    Default Re: crash when delete QTreeWidgetItem*

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: crash when delete QTreeWidgetItem*

    I've created a small test application and it doesn't crash (I use Qt 4.1.5).
    Attached Files Attached Files

Similar Threads

  1. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  2. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38
  3. Delete all members in a QGraphicsItemGroup
    By vmferreira in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2006, 18:47
  4. QListWidget + Delete Key
    By bpetty in forum Newbie
    Replies: 5
    Last Post: 16th August 2006, 20:38
  5. How to explicitely delete a QDir?
    By alan in forum Newbie
    Replies: 2
    Last Post: 13th February 2006, 17:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.