All Projects → VSoftTechnologies → VSoft.WeakReferences

VSoftTechnologies / VSoft.WeakReferences

Licence: other
Weak References for delphi

Programming Languages

pascal
1382 projects

Projects that are alternatives of or similar to VSoft.WeakReferences

VSoft.Messaging
Simple internal application messaging for Delphi
Stars: ✭ 34 (+21.43%)
Mutual labels:  dpmpackage
VSoft.HttpClient
WinHTTP Client for Delphi
Stars: ✭ 18 (-35.71%)
Mutual labels:  dpmpackage
VSoft.CommandLineParser
Simple Command Line Options Parser - part of the DUnitX Project
Stars: ✭ 78 (+178.57%)
Mutual labels:  dpmpackage

VSoft.WeakReference

The idea behind this unit is provide a similar lifecycle to reference counted objects in delphi as WeakReference does in .NET.

Reference counted objects in delphi have some limitations when it comes to circular references, where for example TParent references it's children (via IChild), and TChild references it's parent (via IParent). If we remove any external references to our IParent and IChild instances without first getting the child to remove it's reference to IParent, we would end up with orphaned objects. This is because our IChild and IParent instances are holding references to each other, and thus they never get releaseds.

Usage

Classes that can be weak referenced need to descend from TWeakReferencedObject.

type
    TParent = class(TWeakReferencedObject, IParent)
    ...
    end;

    TChild = class(TInterfacedObject, IChild)
    private
        FParent : IWeakReference<IParent>;
    protected
        procedure SetParent(const parent : IParent);
        function GetParent : IParent;
    public
        property Parent : IParent read GetParent write SetParent;
    end;

implementation

procedure TChild.SetParent(const parent : IParent);
begin
    if parent <> nil then
      FParent := TWeakReference<IParent>.Create(parent)
    else
      FParent := nil;

end;

function TChild.GetParent : IParent;
begin
    result := nil
    if (FParent <> nil) and FParent.IsAlive then
        result := FParent.Data;
end;

In the above example, if the Parent object is destroyed before the child, the weak reference to it in the child object will be marked as nil (so .IsAlive returns false).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].