ARC vs Memory management of records, which is better?

ARC vs Memory management of records, which is better?

ARC is not reliable sometimes. Is bind lifetime of an object to a record more reliable than to an interface, if I want to make an object auto free?

I find there is IManaged interface and Managed record in Spring4d.
This is the declaration in Spring.pas:

IManaged = reference to function: T;

TManaged = class(TInterfacedObject, IManaged)
private
fValue: T;
function Invoke: T; inline;
public
constructor Create; overload;
constructor Create(const value: T); overload;
destructor Destroy; override;
end;

Managed = record
strict private
fValue: T;
fFinalizer: IInterface;
public
class operator Implicit(const value: T): Managed;
class operator Implicit(const value: Managed): T; inline;
property Value: T read fValue;
end;

In Managed, the lifietime of the value is bind to fFinalizer, fFinalizer's lifetime is bind to the record. So the value will be freed when the record is released. Is this better?

Comments

  1. The choice of IManaged vs Managed is based on its usage. When you want a simple assignment but don't access the managed instance that often you might prefer the record because of implicit operator overloading you can assign an instance of T directly to it and pass it somewhere that requires a T but if you want to access members you always have to write .Value.
    IManaged on the other hand requires creating TManaged and passing the to be managed instance there but since its a delegate type you can easily access its members.
    If we had what Allen Bauer called operator hoisting some while ago we could mark that .Value property on the Managed record and all benefits were on the side of the record.

    ReplyDelete

Post a Comment