On Mon, Oct 9, 2017 at 3:54 PM, Maciej Izak <hnb.c...@gmail.com> wrote: > 2017-10-09 14:43 GMT+02:00 Marcos Douglas B. Santos <m...@delfire.net>: >> >> This TAutoFree just "put" the "finally" and call .Free for an array of >> objects or does it something else? >> Could you send the link of the implementation? > > > "One" and "Several" returns IAutoFree interface. Implementation of that > interface contains TObject/array of TObject fields. TObject/array of > TObject is freed when interface goes out of the scope (so yes you can omit > "finally", the "Free" call for allocated objects by TAutoFree is > guaranteed). Documentation: > > https://synopse.info/files/html/api-1.18/SynCommons.html#TAUTOFREE > https://synopse.info/files/html/Synopse%20mORMot%20Framework%20SAD%201.18.html#TITL_130
Hmm, smart. Thanks for that. > please note that FPC behaves different than Delphi, so you can't do the > Delphi trick: > > ===code begin=== > var > o: TFoo; > begin > TAutoFree.One(o, TFoo.Create); // for such code interface is released at > this place in FPC > WriteLn(o.SomeMethod); // AV i FPC > end; // in Delphi returned interface by TAutoFree.One is released here > ===code end=== > > so correct code for FPC is: > > ===code begin=== > var > o: TFoo; > begin > with TAutoFree.One(o, TFoo.Create) do > begin > WriteLn(o.SomeMethod); > o.SomeProperty := 123; > end; > end; > ===code end=== > > Maybe at some point the first code listing will be valid in FPC too (by > usage of new modeswitch SCOPEDINTERFACEDESTROY). IMHO, FPC implementation is more correct because the scope really finished. Using `with` is elegant too. > implementation of TAutoFree can be found here: > > https://github.com/synopse/mORMot/blob/master/SynCommons.pas I saw, thank you again. Best regards, Marcos Douglas _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal