Hey, while working on some managed types using management operators, I've found something interesting out. Temporary values of managed objects get destroyed at the end of the enclosing function.
Example:
TGUniqueObject is a record implementing the management operators that frees an object on the Finalize operator. It also has an overloaded Implicit operator to the class type. For this test I also let it print out free whenever it frees an object:
program example;
{$mode objfpc}{$H+}
uses
heaptrc, Classes, uniqueobject;
type
TUniqueSL = specialize TGUniqueObject<TStringList>;
procedure Foo;
var
s: TStringList;
begin
s := TUniqueSL.Create(TStringList.Create);
s.Add('test');
WriteLn(s.Text);
end;
begin
foo;
WRiteln('end');
end.
Result:
So the free gets called after the procedure foo exits and not after the memory object in question (the temporary TUniqueSL instance) gets "out of scope" (i.e. is not accessible anymore).
This of course has the neat side effect that using this code above I can create temporary instances without the need of using a variable of type TUniqueSL, which will automatically get cleaned up.
So I have a question about this: Is this intended behavior and can I rely on this, or is this a side effect that might change in the future?