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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds