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