Comment

Comment by undreren

No. Delphi offers reference counting, but only on interface variables. It's a huge difference really, because if your last interface variable that points to an object goes out of scope, the object is freed from memory. Even if you have raw class pointers to it.

Example:

    IMyInterface = interface
        procedure Foo;
    end;

    TMyClass = class(TInterfacedObject, IMyInterface)
        procedure Foo;
    end;

    procedure TMyClass.Foo;
    begin
        WriteLn('Foo');
    end;

    procedure DoTheFoo(AObj: IMyInterface);
    begin
        AObj.Foo;
    end;

    procedure SomeMethod;
    var
        LObj: TMyClass;
    begin
        LObj := TMyClass.Create;
        DoTheFoo(LObj);
        LObj.Foo; // <-- Will cast nil pointer exception,
                  // as object was freed when AObj in DoTheFoo went
                  // out of scope
    end;
I work with Delphi in my day job.
parent undreren 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton