When an object that is created within a function and the function is completed, what happens to the object if it wasn’t explicitly destroyed?
Do all variables need to be destroyed when they fall out of scope or are they taken care of when they fall out of scope?
So for example, what happens to locallist after custom_function has been called?
function TForm1.custom_function(string: test_string): boolean; var locallist: TStringList; begin locallist := TStringList.Create; // do a bunch of stuff here, but don't destroy locallist return true; end;
You will get a memory leak.
The proper pattern is
Also if you need to test later if the object has been freed, then use FreeAndNil(myObject). It will set the variable to nil as well, so you can test it later.