Auto memory or objects destroyer (pseudo garbage collection) lazarus / 31-LCL ---------------------------------------------------------------------- vladvons · 2013-06-23 20:04 Under Delphi to avoiding "Try-Finally" deep nesting code style i used ISafeGuard (JCL-> JclSysUtils.pas). http://wiki.delphi-jedi.org/wiki/JCL_Help:IMultiSafeGuard A safeguard is an interface which gets associated with the resources and ensures that when execution leaves the scope at which the safeguard variable is declared, the resources are released Code: [Select]function MyFunction(): Integer; var   SG: IMultiSafeGuard;   StringList1, StringList2: TStringList; begin   StringList1 := Guard(TStringList.Create(), SG) as TStringList;   StringList1.Add("Hello");   StringList2 := Guard(TStringList.Create(), SG) as TStringList;   StringList2.Add("World");   Result := StringList1.Count() + StringList2.Count() ;   // no need to free it!   // StringList1.Free();   // StringList2.Free();  end; What has FreePascal or Lazarus for such purpose? ---------------------------------------------------------------------- taazz · 2013-06-24 02:41 As far as I know you can use that jcl unit and functions as is in fpc as well. Have tried it and got any problems? ---------------------------------------------------------------------- Leledumbo · 2013-06-24 11:54 I've tested that trick before (it uses COM reference counting feature), though not from JCL unit and it works fine in FPC. ---------------------------------------------------------------------- vladvons · 2013-06-27 21:29 Just couldnt find existing class in FreePascal / Lazarus like a ISafeGuard from JCL, so i tried to compile package jcl\packages\fpc\jcl.lpk under FPC, but too many dependencies and errors occured. After all i only cut ISafeGuard class source code from JCL and successfully compiled. I know about COM & Interfaces in pascal, but wonder why such convenient trick for releasing objects is not prevalent and not included into FreePascal RTL. ---------------------------------------------------------------------- Leledumbo · 2013-06-28 00:25 Quotebut wonder why such convenient trick for releasing objects is not prevalent and not included into FreePascal RTLNot everyone likes the concept of garbage collection, including this one even though it's not really a garbage collection. I prefer managing my own memory rather than relying on something that runs unpredictably. Garbage collection makes complexity calculation difficult. ---------------------------------------------------------------------- Cyrax · 2013-06-28 06:05 Quote from: vladvons on June 27, 2013, 11:29:08 pmJust couldnt find existing class in FreePascal / Lazarus like a ISafeGuard from JCL, so i tried to compile package jcl\packages\fpc\jcl.lpk under FPC, but too many dependencies and errors occured. After all i only cut ISafeGuard class source code from JCL and successfully compiled. I know about COM & Interfaces in pascal, but wonder why such convenient trick for releasing objects is not prevalent and not included into FreePascal RTL. To get JCL to compile under FPC, grab its sources and latest patch for it from here : http://issuetracker.delphi-jedi.org/view.php?id=5454 ---------------------------------------------------------------------- vladvons · 2013-07-01 07:49 I have problem with patching. In my case i put  patch file into JCL root directory \CommonLib\jcl\jcl\ and run it with GnuWin32 patcher. patch.exe -i "06.09.2012+-+patch+against+jcl+trunk+revision+3864+- +fpc+trunk+revision+22332.patch" Code: [Select]can't find file to patch at input line 5 Perhaps you should have used the -p or --strip option? The text leading up to this was: -------------------------- |Index: packages/fpc/Jcl.lpk |=================================================================== |--- packages/fpc/Jcl.lpk       (revision 3869) |+++ packages/fpc/Jcl.lpk       (working copy) -------------------------- File to patch: nevertheless file "packages/fpc/Jcl.lpk" exists and i even can open it with editor from JCL root directory: notepad.exe  packages/fpc/Jcl.lpk What im doing wrong? ---------------------------------------------------------------------- BigChimp · 2013-07-01 07:54 The FPC/Lazarus supplied patch.exe on Windows is notoriously fickle. You might want to fiddle with the -p0, -p1 etc options. I've written (free, open source) PatchWrangler to try and automatically fix these issues. https://bitbucket.org/reiniero/patchwrangler/downloads