Solved

Delphi interface reference counting

Posted on 2002-07-22
4
273 Views
Last Modified: 2010-04-04
    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
Comment
Question by:mythos128
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
 
LVL 6

Expert Comment

by:swift99
Comment Utility
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
 
LVL 20

Accepted Solution

by:
Madshi earned 150 total points
Comment Utility
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
 
LVL 6

Expert Comment

by:swift99
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now