1

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

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.

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.

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.

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

efotinis
  • 14,565
  • 6
  • 31
  • 36

2 Answers2

2

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.

ChrisN
  • 16,635
  • 9
  • 57
  • 81
0

You don't want to directly edit the text in the TVITEM struct, the results are undefined. Instead, use the TVM_SETITEM message, or equivalently, use the TreeView_SetItem() macro defined in windowsx.h.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589