I have a few forms where I have a TListBox component that I fill runtime.
My question is how can I best free the items that I add runtime?
- By using the owner? Form or ListBox?
- Or freeing them myself?
- or a different way?
Below an example how I fill my listbox:
procedure TForm1.LoadList;
var
item: TListBoxItem;
begin
myList.Clear;
myList.BeginUpdate;
try
with myQuery do
begin
First;
while not eof do
begin
item := TListBoxItem.Create(nil);
try
item.Tag := FieldByName(myIDField).AsInteger;
item.Text := FieldByName(myDescriptionField).AsString;
myList.AddObject(item);
finally
Next;
end;
end;
end;
finally
myList.EndUpdate;
end;
end;
I do have noticed that creating the list may take a bit longer when I set the owner of the item. Also when the I call ListBox.Clear and the list was filled with items with no owners the list still gets cleared correctly. So does this mean the owner of the items get set when I use AddObject to add them to the ListBox?
Also I Free the form with the close action:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := TCloseAction.CaFree;
end;
I don't know if this changes things how to free the items in my list?