SkillAgentSearch skills...

SmartPointers

This repo includes all different versions of SmartPointer in Delphi to simulate a garbage collector.

Install / Use

/learn @AliDehbansiahkarbon/SmartPointers
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

SmartPointers (Garbage collector simulator) for Delphi.

This repo includes 7 different implementations of SmartPointers in Delphi to simulate a simple garbage collector.

In this way you will bind your objects to a reference counting supported object like an interface or a record and this object will automatically free the referenced object at the end of the scope. You just create your object and don't worry about freeing anymore.

There is a sample vcl app to demonstrate the usage.

Simple Usage

presumptive class

  TPerson = class
    FName: string;
    FAge: Integer;
  public
    constructor Create; reintroduce; overload;
    constructor Create(const AName: string; const AAge: Integer); reintroduce; overload;
    destructor Destroy; override;
    procedure Birthday;
    property Name: string read FName write FName;
    property Age: integer read FAge write FAge;
  end;

Version 1, 2, 3, 4 (usages are same but different implementations)

var Person1: ISmartPointer1{or 2-4}<TPerson> := TSmartPointer1{or 2-4}<TPerson>.Create(TPerson.Create('Alex', 50));
 Person1.Ref.Birthday; // use the created object
 //done, no need to free the object.

Version 5

var
  LvPerson1: ISmartPointer5<TPerson>;
begin
  LvPerson1 := TSmartPointer5<TPerson>.Create(TPerson.Create('Person1', 45));
  LvPerson1.Birthday; // direct member access, do something with object.
  // done, no need to free.
end;

Version 6

var
  LvPerson1: TPerson;
begin
  LvPerson1 := SmartPointer6<TPerson>(TPerson.Create('Ali', 25));
  LvPerson1.Birthday;// do anything
  //done, no need to free.
end;

Version 7

var
  x: TSmartPointer7<TLifetimeWatcher>;
begin
  x := TLifetimeWatcher.Create(procedure begin ShowMessage('Destroying...'); end); // pass anonymouse method just for watching the life cycle of the object(it's not mandatory.)
  ShowMessage(X.Value.Birthday); // use the object.
  //done, noe need to free
end;
<hr> <p align="center"> <img src="https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/dlogonew-5582740.png?resize=254%2C242&ssl=1" alt="Delphi"> </p> <h5 align="center"> Made with :heart: on Delphi </h5>

Related Skills

View on GitHub
GitHub Stars23
CategoryDevelopment
Updated4mo ago
Forks7

Languages

Pascal

Security Score

72/100

Audited on Nov 27, 2025

No findings