It may also be useful for queries.
Instead of writing:
var aMale: TSQLBaby; ... aMale := TSQLBaby.CreateAndFillPrepare(Client, 'Name LIKE ? AND Sex = ?',['A%',ord(sMale)]); try while aMale.FillOne do DoSomethingWith(aMale); finally aMale.Free; end;
We may write:
var aMale: TSQLBaby; ... TAutoFree.Create(aMale,TSQLBaby.CreateAndFillPrepare(Client, 'Name LIKE ? AND Sex = ?',['A%',ord(sMale)])); while aMale.FillOne do DoSomethingWith(aMale);
Without the need to write the
try ... finally block.
TSQLRecord.AutoFree() overloaded methods in
mORMot.pas for the several use cases, and the associated
IAutoFree types as defined in
Note that you can handle several local variables in a single
Be aware that it does not introduce some kind of magic garbage collector, as
available in C# or Java. It is not even similar to the
model used by Apple and the Delphi NextGen compiler.
It is just some syntaxing sugar creating a local hidden
interface, which would be released at the end of the local method, and also
release all associated class instances.
So the local class instances should stay in the local scope, and should not be sent and stored in another process: in such cases, you may encounter access violation issues.