Link to home
Start Free TrialLog in
Avatar of mythos128
mythos128

asked on

Delphi interface reference counting

    Please explain how Delphi manages interface reference counting. It seems like it automatically calls _AddRef when I assign a value to an iref, and calls _Release when it goes out of scope (my explicit _Release results in Access Violation). When I pass an iref to some procedure (var or out parameter), again Delphi knows that it needs to call _Release. But how? The procedure might have not assigned anything. The same seems to happen with irefs - class members: when I call _Release and set iref to nil in, say, FormDestroy, it works, but then I get Access Violation as if Delphi calls _Release once more.
Avatar of Madshi
Madshi

The solution is very simple: Don't never ever call _Release/_AddRef (that's why there is a "_" before those methods), Delphi takes care of everything, automatically.

Look, if you're an experienced programmer, you know that Delphi strings are also 100% automatically handled (allocated, reallocated & freed) by Delphi, so it should come to no surprise that Delphi can do the same with interfaces.

There's just one thing you have to be careful about: Mixing classes and interfaces is difficult. A little example:

type
  IExample : interface
    procedure ExampleMethod;
  end;

type
  TIExample : class (TInterfacedObject, IExample)
    procedure ExampleMethod;
  end;

procedure TIExample.ExampleMethod;
begin
  [...]
end;

procedure TestFunction;
var exclass : TIExample;
    exintf  : IExample;
begin
  exclass := TIExample.Create;  // reference count 0
  exclass.ExampleMethod;        // still 0
  exclass.Free;                 // still 0
  exclass := TIExample.Create;  // again 0
  exintf := exclass;            // reference count 1
end;

In the latter case, don't free the class, because the interface frees the class, when the reference count goes from 1 to 0, which will happen automagically at the end of the TestFunction.

You see the little twist?

Regards, Madshi.
Correction:

Interfaces do not create any executable code, so the simple use of an interface does not guarantee automatic freeing of objects.  

Reference counting is handled by the TInterfacedObject class.  The class methods _AddRef and _Release are defined for all objects inheriting from this class.  If you do not inherit from TInterfacedObject then you need to implement your own _AddRef and _Release methods.

All objects that implement the _AddRef and _Release methods are recognized by the compiler, and the code to invoke those methods is implicitly requersted by use of the assignment := operator, so you do not need to explicitly invoke Free in many circumstances.

_Release typically invokes Free once the object's reference count is 0, but your implementation of _Release could do almost anything in theory.
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are correct.  The _AddRef and _Release methods are not recognized by the compiler - they are embedded in the VCL implementation of calls to the interface.  I must have been smoking something really interesting when I wrote that - now if I only knew what it was ...   :o)