I have a an object that extends from TFrame. This contains a TCombobox.
I want to make sure i free any associated objects when my frame is destroyed. However when my destructor gets run i can access the combobox, but it's items have been wiped out.
What would do this? How can i access the items in the destructor?
my destructor looks like this;
destructor TfraImportAttachments.Destroy;
begin
MessageDlg(IntToStr(cboCategory.Items.count), mtInformation, [mbOK], 0);
FreeObjects(cboCategory.Items);
inherited;
end;
A DestroyHandle
may have reached the combobox before the destructor. Then the Items are gone because they are not saved to the TCustomComboBox.FSavedItems
list in TCustomComboBox.DestroyWnd
.
The combobox Delphi object still exists, but the actual control (the one that is accessed through the window handle) is gone. By accessing ComboBox.Items the VCL recreates the actual control so it can retrieve the items, but that new control doesn't have any items, so Items.Count
returns 0.
A solution would be to put the items into the combobox and a TObjectList
, TList
or TList<TObject>
depending on what you want to do with them. So the ownership is in the "code" list while the items are still referenced in the combobox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With