I've got a function in Pascal which returns a StringList
as the result. How do I free it correctly? As it's not a global variable I can't free it in the Form.FormDestroy
procedure. And freeing it after returning it doesn't work either (that should be allowed though, LOL).
In general, is there a way to free all objects (including the ones that the form has no control over) as soon as the program is closed?
@TomBrunberg is right, his comment solved the problem:
Take a reference to the returned TStringList
and use that ref to free it:
var
sl: TStringList;
begin
sl := fnThatReturnsAStringList;
UseTheList(sl);
sl.Free;
end;
Another possibility, in case you call the function only because of side effects of that call, without any interest in the returned TStringList
, you may even free it simply by calling:
begin
fnThatReturnsAStringList.Free;
end;