Recent

Author Topic: Auto memory or objects destroyer (pseudo garbage collection)  (Read 7007 times)

vladvons

  • Jr. Member
  • **
  • Posts: 65
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?
« Last Edit: June 28, 2013, 07:43:50 am by vladvons »
Windows 7, Ubuntu 12.04, Lazarus 1.2.2, FPC 2.6.4, PostgreSQL 9.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Auto memory or objects destroyer
« Reply #1 on: June 24, 2013, 04:41:57 am »
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?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Auto memory or objects destroyer
« Reply #2 on: June 24, 2013, 01:54:09 pm »
I've tested that trick before (it uses COM reference counting feature), though not from JCL unit and it works fine in FPC.

vladvons

  • Jr. Member
  • **
  • Posts: 65
Re: Auto memory or objects destroyer
« Reply #3 on: June 27, 2013, 11:29:08 pm »
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.
« Last Edit: June 27, 2013, 11:31:21 pm by vladvons »
Windows 7, Ubuntu 12.04, Lazarus 1.2.2, FPC 2.6.4, PostgreSQL 9.2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Auto memory or objects destroyer
« Reply #4 on: June 28, 2013, 02:25:31 am »
Quote
but wonder why such convenient trick for releasing objects is not prevalent and not included into FreePascal RTL
Not 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

  • Hero Member
  • *****
  • Posts: 836
Re: Auto memory or objects destroyer
« Reply #5 on: June 28, 2013, 08:05:46 am »
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.

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

  • Jr. Member
  • **
  • Posts: 65
Re: Auto memory or objects destroyer (pseudo garbage collection)
« Reply #6 on: July 01, 2013, 09:49:28 am »
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?
Windows 7, Ubuntu 12.04, Lazarus 1.2.2, FPC 2.6.4, PostgreSQL 9.2

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Auto memory or objects destroyer (pseudo garbage collection)
« Reply #7 on: July 01, 2013, 09:54:35 am »
The FPC/Lazarus supplied patch.exe on Windows is notoriously fickle.
You might want to fiddle with the -p0, -p1 etc options.

<ad>I've written (free, open source) PatchWrangler to try and automatically fix these issues.
https://bitbucket.org/reiniero/patchwrangler/downloads
</ad>
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018