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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds