OK I've attached the same project with all the hairy pieces removed and a few light comments. For any farther comments ask.
Hi, i think now you missed one ...
I think this is better:
procedure TForm1.HandleCommand;
begin
case Edit1.Text of
'Dynamic' : begin
if Assigned(FDynamicPopup) then FreeAndNil(FDynamicPopup);// this is must otherwise you have a memmory leak
FDynamicPopup := TDynamicPopup.Create(Self); // <-- Here
FDynamicPopup.FillItems;
end;
else begin
ShowMessage('Invalid Command');// Showmessage should be enough.
Edit1.Text:='';//<-- bad choice! what happens if some one has typed Dynamik instead of Dynamic why force him to retype everything?
end;
end;
end;
This way it's also freed when the form closes.
True! by assigning the owner in the TDynamicPopup constructor you instruct the owner to destroy the control when it destroys it self.
[Edit]
Not Quite,
Better do a
if Assigned(FDynamicPopup) then FreeAndNil(FDynamicPopup);// this is must otherwise you have a memmory leak
in FormDestroy
No don't. Either set the owner in the constructor aka your previous comment in the quoted post or add the check on the destructor not both.
It is not problematic per see. To make things clear there will be no problem if you do both except a small waste of cpu cycles. look at it like this
1) you call TDynamicPopup.Create(aComponent); this means that TDynamicpop adds it self on the acomponent destroylist
2) acomponent in order to protect it self from GPFs or as known in fpc sigsegv adds it self in the dynamicpopup freenotification list
each time the tdynamicpopup is freed it walked down all the components in its freenotification list and notifies them about the imminent destruction of the TDynamicPopup. In this process the Owner gets a notification that a control is about to be destroyed and searches if that control is in its own lists if it is found it is removed moving the list items up the ladder as needed. And here you have a small conflict is the component destroyed by the owner or it has been destroyed by something else? If you add code to destroy it on the destructor you are circumventing the ownership mechanism and the search and remove is executed if you do not destroy it your self and let the owner mechanism do its job this is avoided.
The rest of the details are left as a small exercise for the reader. What is the optimum choice for this situation add self in the owner or add the freeandnil call in the destructor and why? What needs to be changed to make the other choice optimum?