Is TListView OwnerData OnData event leaking memory?

397 views Asked by At

I am using TListView in virtual mode (OwnerData set to true) and that is simple enough. TListView is in vsReport view style and uses main Caption and SubItems. The event code for OnData is simple (the code is C++ Builder but you'll figure it out if you use Delphi):

void __fastcall TForm1::ListView1Data(TObject *Sender, TListItem *Item)
{
Item->Caption     = MyList[Item->Index].Name;
Item->SubItems->Add(MyList[Item->Index].Status);
}

All good so far, but I am puzzled by SubItems->Add - is that a memory leak to continuously add SubItems like that (also goes for Caption)?

Am I supposed to free the memory in an another event (which I haven't found yet)?

2

There are 2 answers

0
David Heffernan On BEST ANSWER

No it is not a memory leak. The framework allocated the SubItems object, and it deallocates it when it has finished using it.

0
Remy Lebeau On

No. Internally, TListView maintains a single physical TListItem object when running in virtual mode. It reuses and clears that object whenever the OnData event needs to be triggered. You do not need to free any of the TListItem data manually.