Page 1 of 1

wxListCtrl::SetItemPtrData: Who owns the memory?

Posted: Thu Mar 26, 2020 3:09 pm
by jpo234
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.

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

Posted: Thu Mar 26, 2020 3:34 pm
by PB
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...).

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

Posted: Thu Mar 26, 2020 5:17 pm
by jpo234
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.