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: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Live Event Date: August 19, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You deployed your app with the Bluemix PaaS and it's gaining some serious traction, so it's time to make some tweaks. Did you design your application in a way that it can scale in the cloud? Were you even thinking about the cloud when you built the app? If not, chances are your app is going to break. Check out this upcoming eSeminar to learn various techniques for designing applications that will scale successfully in Bluemix, for the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds