Release tree-items' data

It is common use to associate user data with a tree item. If it is a pointer to some class the function that gracefully releases all memory is needed. The one is given below. It takes as an argument a handle of a tree item and deletes all CSomeXObjets (user objects) in all subitems. If You want to release the whole tree call: ReleaseTreeItem( TVI_ROOT );


void CTreeCtrlX::ReleaseTreeItem(const HTREEITEM hti)
{
	if(hti == NULL)
		return;

	HTREEITEM hti_ch = GetChildItem( hti );

	while( hti_ch != NULL )
	{
		CSomeXObjets* pObject = ( CSomeXObjets* )GetItemData( hti_ch );
		delete pObject;

		ReleaseTreeItem( hti_ch );
		hti_ch = GetNextSiblingItem( hti_ch );

	} //whil

}; //ReleaseTreeIte




Comments

  • Combining Loj's and Freeman's into the optimal solution

    Posted by Legacy on 09/20/1999 12:00am

    Originally posted by: Tomaz Stih

    When writing tiered app the domain level will
    almost certainly contain a copy of your tree ctrl data
    as demonstrated in Zoran M. Todorovic's "Connectiong Container to Tree" article.

    Thus direct deletion of objects is a bad practice and
    one should avoid it... The good solution would be to
    delete the tree item and then instruct (in on delete
    event as proposed by Freeman) your domain level to
    execute the deletion on the pointer.

    Tomaz

    Reply
  • What if...

    Posted by Legacy on 12/30/1998 12:00am

    Originally posted by: Pawel Loj

    Well, I was using different approaches to solve this 
    
    problem but NONE of these worked well. The whole problem is
    in message-driven windows architecture (one basically has no
    control over total message's order, so I am always afraid to
    use something that really does not exist yet because it has
    just been deleted while handling some message).

    IMHO, it is better to store all item's data in some
    external container (STL is good for this purpose) and
    purge the container AFTER all items are deleted and no one
    will call anything that would use the stuff.

    It easy, it quick and it is nice!

    Pawel

    Reply
  • An alternative...

    Posted by Legacy on 10/22/1998 12:00am

    Originally posted by: Russ Freeman

    Instead why not handle the reflected message TVN_DELETEITEM, you coudl then add a function as follows:
    
    void CTreeCtrlX::OnDeleteitem(NMHDR* pNMHDR, LRESULT* pResult)
    {
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    CItem *pItem = reintrepret_cast<CItem*>( GetItemData() );
    if( pItem )
    {
    delete pItem;
    }
    }

    This way your client code will not need to be modified to delete teh items. Also, if you ensure all of your items are derived from CObject then you can write a generic OnDeleteItem fucntion.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • What should you consider when planning your backup and recovery strategy? Read this enlightening report to find out. Among other matter, it explores the potential costs of not taking action, and shows how to justify your investment in an effective, integrated backup and recovery solution.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds