Solved

Delphi interface reference counting

Posted on 2002-07-22
4
277 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
ID: 7171083
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
ID: 7175599
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
ID: 7176423
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
ID: 8574957
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Communication Between RC4 Delphi <-> PHP 3 100
Printing problem 2 81
Tviruailstringtree sort multi columns on header click 1 54
Delphi and Access based Enumeration 9 47
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

867 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

16 Experts available now in Live!

Get 1:1 Help Now