Release tree-items’ data

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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)

	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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read