wxListCtrl::SetItemPtrData: Who owns the memory? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
jpo234
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Feb 25, 2020 11:34 am

wxListCtrl::SetItemPtrData: Who owns the memory?

Post by jpo234 » Thu Mar 26, 2020 3:09 pm

Hello all,
if I put a pointer to an instance of something into a wxListCtrl:
  • Who owns the memory? I assume it's the caller of wxListCtrl::SetItemPtrData since there is no deleter passed along with the pointer...
  • If so, when is it save to delete the object pointed to by the pointer?
  • Is there a best practice to manage these pointers?
Thanks in advance
J.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2245
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxListCtrl::SetItemPtrData: Who owns the memory?

Post by PB » Thu Mar 26, 2020 3:34 pm

I think wxListCtrl just stores this data and never does anything with them. If the ptrdata contains a pointer to a dynamically allocated memory (it can be just integer as well), the application programmer is responsible for freeing the memory when it makes sense.

If the lifetime of the objects stored in ptrdata matches the lifetime of listctrl items, I would just release the memory when the items are no longer needed (an item is deleted from wxListCtrl, wxListCtrl is destroyed...).

jpo234
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Feb 25, 2020 11:34 am

Re: wxListCtrl::SetItemPtrData: Who owns the memory?

Post by jpo234 » Thu Mar 26, 2020 5:17 pm

PB wrote:
Thu Mar 26, 2020 3:34 pm
I think wxListCtrl just stores this data and never does anything with them. If the ptrdata contains a pointer to a dynamically allocated memory (it can be just integer as well), the application programmer is responsible for freeing the memory when it makes sense.

If the lifetime of the objects stored in ptrdata matches the lifetime of listctrl items, I would just release the memory when the items are no longer needed (an item is deleted from wxListCtrl, wxListCtrl is destroyed...).
Thanks. That's what I thought and was afraid of.

Post Reply