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.
See the TSQLRecord.AutoFree()
overloaded methods in
mORMot.pas
for the several use cases, and the associated
TAutoFree
/ IAutoFree
types as defined in
SynCommons.pas
.
Note that you can handle several local variables in a single
TSQLRecord.AutoFree()
or TAutoFree.Create()
initialization.
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 ARC
memory
model used by Apple and the Delphi NextGen compiler.
It is just some syntaxing sugar creating a local hidden IAutoFree
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.
You may likely to take a look at the
updated reference documentation.
Or discuss in our
forum, as usual!