[Lazarus] IUnknown and reference counting

Hans-Peter Diettrich DrDiettrich1 at aol.com
Thu Mar 21 18:33:33 CET 2013


Kostas Michalopoulos schrieb:
> Thanks, i understand that. My question is, can i somehow make FPC to add 
> AddRef/Release calls also to TBlah so i can use refcounting with a class 
> just like i would use it with an interface?

When you declare IBlah and implement it in TBlah, you can use:

var MyBlah: IBlah; //instead of TBlah!
...
   MyBlah := TBlah.Create as IBlah;

Now MyBlah will be destroyed when the last reference to it goes away.

> Is there some technical 
> limitation on why this is only done with interfaces and not all objects 
> that descend from a refcounted interface?

Reference counting adds some runtime overhead. If you want all your 
objects reference counted, then use interface references instead of 
object references in your code, as outlined above.

DoDi





More information about the Lazarus mailing list