SmartPointer
Classe para utilização como Garbage Collector
- Active · last change 5-1-2025
- ★ 1 on GitHub
README
SmartPointer for Delphi
This unit provides a generic SmartPointer class in Delphi, designed to manage object lifecycle automatically, acting as a garbage collector. It simplifies memory management by ensuring that objects are automatically freed when they are no longer needed, thus preventing memory leaks.
Overview
The SmartPointer class is a generic wrapper around Delphi objects that implements reference-counting. It ensures that the object it manages is automatically destroyed when the reference count drops to zero. This class can be used in any Delphi project that requires simplified memory management, removing the need to manually free objects.
Features
- Automatic object destruction when the reference count reaches zero.
- Supports any class that has a parameterless constructor (
constructorconstraint). - Prevents memory leaks by managing the object lifecycle.
- Can be created with a new object or assigned an existing one.
Usage
Installation
To use the SmartPointer class in your project, include the Classe.SmartPointer unit in your Delphi application:
uses
Classe.SmartPointer;Creating a SmartPointer
Option 1: Automatic Object Creation
You can create a SmartPointer that will automatically instantiate the object:
var
MyPointer: ISmartPointer<TMyClass>;
begin
MyPointer := TSmartPointer<TMyClass>.Create;
MyPointer.DoSomething; // Access your object as needed
end; // Object is automatically freed when the reference goes out of scopeOption 2: Using an Existing Object
Alternatively, create the SmartPointer with an already instantiated object:
var
MyPointer: ISmartPointer<TMyClass>;
begin
MyPointer := TSmartPointer<TMyClass>.Create(TMyClass.Create);
MyPointer.DoSomething; // Access the object through the smart pointer
end; // The object is freed automaticallyOption 3: Using an Existing Object with Separate Instantiation
You can also pass a pre-existing, separately instantiated object to the SmartPointer:
var
MyObject: TMyClass;
MyPointer: ISmartPointer<TMyClass>;
begin
MyObject := TMyClass.Create;
MyPointer := TSmartPointer<TMyClass>.Create(MyObject);
MyPointer.DoSomething; // Access the object through the smart pointer
end; // The object is freed automaticallyAnother example: Using TClientDataSet and Accessing Fields
The SmartPointer can also be used with database components, such as TClientDataSet. Here's an example showing how to create and access fields in TClientDataSet:
uses
Data.DB, Datasnap.DBClient, Classe.SmartPointer;
var
ClientDataSetPointer: ISmartPointer<TClientDataSet>;
begin
ClientDataSetPointer := TSmartPointer<TClientDataSet>.Create(TClientDataSet.Create(nil));
// Note: You need to define the fields here before calling CreateDataSet
ClientDataSetPointer.CreateDataSet;
ClientDataSetPointer.Append;
ClientDataSetPointer.FieldByName('MyField').AsString := 'Hello, World!';
ClientDataSetPointer.Post;
// Access data
ShowMessage(ClientDataSetPointer.FieldByName('MyField').AsString);
end; // TClientDataSet is freed automaticallyAccessing the Object
To access the object managed by the SmartPointer, you can directly call methods or access properties without needing to invoke any specific method. The SmartPointer automatically handles the reference to the object.
MyPointer.DoSomething; // No need to call Invoke
