<p>I have a Win32 TreeCtrl where the user can rename the tree labels. I process the TVN_ENDLABELEDIT message to do this.</p>

<p>In certain cases I need to change the text that the user entered. Basically the user can enter a short name during edit and I want to replace it with a longer text.</p>

<p>To do this I change the pszText member of the TVITEM struct I received during TVN_ENDLABELEDIT. I do a pointer replace here, as the original memory may be too small to do a simple strcpy like operation.</p>

<p>However I do not know how to deallocate the original pszText member. Basically because it's unknown if that was created with malloc() or new ... therefore I cannot call the appropriate deallocator. Obviously Win32 won't call the deallocator for the old pszText because the pointer has been replaced. So if I don't deallocate, there will be a memory leak.</p>

<p>Any idea how Win32 allocate these structs and what is the proper way to handle the above situation?</p>

Unless you're using
```
LPSTR_TEXTCALLBACK
```
, the tree-view control is responsible for allocating the memory, not your code, so you shouldn't change the value of the
```
pszText
```
pointer.
To change the item's text in your
```
TVN_ENDLABELEDIT
```
handler, you can use
```
TreeView_SetItem
```
, then return 0 from the handler.