In our existing code we have a bunch of these where a form is created with MainForm as the owner (say instead of nil) yet we free it explicitly.
function SomeFunc(): Boolean;
var
form: TMyForm; // subclasses TForm
begin
with TMyForm.Create(Application.MainForm) do
try
ShowModal;
Exit(True);
finally
Free;
end
end;
Can this cause any form of bug or crash, or is it safe?
I can't seem to sort it out by reading the doc:
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Classes.TComponent.Owner
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Classes.TComponent.Create
FreeAndNil()
of locally created & destroyed object is just as unnecessary as passing an owner. More importantly, bearing in mind OP code, you should neverFreeAndNil()
an uninitialised local variable. – BelcherTrue
; (Since only one result is possible, this shouldn't be a function.) – BelcherExit(True)
here??? On a side note, does my answer addresses your question? – Stinger