Results 1 to 6 of 6

Thread: Deleting from TreeWidget... Memoryleak

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Deleting from TreeWidget... Memoryleak

    It's not a slot or a signal though. It has nothing to do with either. It's not connected to anything.

    Let me show you a more detailed code for that, this is updated with logs so we can see better, there must be a bug in Qt or something because this makes no sense:
    Qt Code:
    1. inline static void SafeDeleteItem( QTreeWidgetItem *pItem )
    2. {
    3. QTreeWidgetItem *pParent = (QTreeWidgetItem*)pItem->parent();
    4. LOG("SafeDelete Text: %s (%x)", Q2A( pItem->text( 0 ) ), pItem );
    5. assert( pParent );
    6. int idx = pParent->indexOfChild( pItem );
    7. LOG("index=%d; ParentText: %s",idx, Q2A( pParent->text(0) ));
    8. LOG("Can We access pItem? %s %s", Q2A( pItem->text(0) ), Q2A(pItem->parent()->text(0)));
    9. pParent->takeChild( idx );
    10. LOG("Can We access Parent? %s", Q2A( pParent->text(0) ));
    11. LOG("Can We access pItem? %s %s", Q2A( pItem->text(0) ), Q2A(pItem->parent()->text(0)));
    12.  
    13. ASSERT( !pItem->parent() );
    14. ASSERT( pParent->indexOfChild ( pItem ) == -1 );
    15. ASSERT( !pItem->treeWidget() );
    16. LOG("parent=%d",pParent->treeWidget() );
    17. ASSERT( pParent->treeWidget() );
    18.  
    19. delete pItem; // mem leak
    20.  
    21. LOG("SafeDeleteOut" );
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    OUTPUT in log:

    22:55:23 SafeDelete Text: bob (2e25d20)
    22:55:23 index=1; ParentText: CorrectParentText
    22:55:23 Can We access pItem? bob CorrectParentText
    22:55:23 Can We access Parent? CorrectParentText
    22:55:23 Can We access pItem? bob CorrectParentText
    22:55:23 Debug assert failed on line 239 of testone.h.

    How can I access pItem->parent()->text(0), but then, the next line assert fails ON:
    ASSERT(!pItem->parent())

    I don't understand this.

    EDIT:
    If I remove the asserts, it goes by this function OUT... and then it comes back to this function again in the next item in the list... and crashes on the SECOND
    LOG("Can We access pItem? %s %s", Q2A( pItem->text(0) ), Q2A(pItem->parent()->text(0)));

    Like as if takeChild destroys the parent or something.
    Last edited by VireX; 1st June 2007 at 04:21.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Deleting from TreeWidget... Memoryleak

    What calls that "DeleteUser" method? How does the structure of the tree look like? Do those items really have parent items?

  3. #3
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Deleting from TreeWidget... Memoryleak

    As you can see by the log data:

    22:55:23 SafeDelete Text: bob (2e25d20)
    22:55:23 index=1; ParentText: CorrectParentText
    22:55:23 Can We access pItem? bob CorrectParentText
    22:55:23 Can We access Parent? CorrectParentText
    22:55:23 Can We access pItem? bob CorrectParentText
    22:55:23 Debug assert failed on line 239 of testone.h.

    This means, that bob has a parent named CorrectParentText, and so I don't understand how an assert can fail, when it calls the parent() even though we just did it the line before, unless its some sort of weird bug in takeChild causing memory corruption or something.

    what the last two lines mean is this:
    pItem->parent()->text( 0 ); <--- no crash.
    pItem->parent() <--- crash.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Deleting from TreeWidget... Memoryleak

    Correct me if I'm wrong but it seems to me the behaviour is perfectly fine with what your code looks like. The thing you do is more or less like the following:
    1. find index of pItem which is child of pParent
    2. pParent->takeItem(pItem)
    3. access pItem->parent()
    4. app asserts

    The thing is that in (2) you remove the item from its parent, thus it doesn't have a parent anymore, therefore accessing the parent before (2) is fine, but after (2) causes the assert to fire because the parent is not valid anymore (it's null).

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
  •  
Qt is a trademark of The Qt Company.