Solved

Delphi interface reference counting

Posted on 2002-07-22
4
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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