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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds