• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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.
0
mythos128
Asked:
mythos128
  • 2
  • 2
1 Solution
 
MadshiCommented:
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.
0
 
swift99Commented:
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.
0
 
MadshiCommented:
Hi swift99...

I agree with you with one exception:

>> All objects that implement the _AddRef and _Release methods are recognized by the compiler

That is not correct. If you do this:

type
  TSomeClass = class
    function _AddRef  : integer; virtual; stdcall;
    function _Release : integer; virtual; stdcall;
  end;

Delphi does *NOT* "recognize" those functions in this case and will never call them automatically because of an assignment or because of any other reason.

Delphi recognizes *interfaces*. Each class, which is implementing an interface, *has* to implement _AddRef and _Release. And even in this case _AddRef and _Release are only automatically called, if you work with the interface, not if you work with the class. See the code example in my previous comment.

Regards, Madshi.
0
 
swift99Commented:
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)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now