[Lazarus] IUnknown and reference counting

Sven Barth pascaldragon at googlemail.com
Thu Mar 21 14:54:55 CET 2013


Am 21.03.2013 14:48, schrieb Kostas Michalopoulos:
> 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?

No. At least not yet (when a new Delphi is released which might be 
featuring reference counted objects than this is up for discussion again)

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

It's more a logical problem. Assume you have a "TInterfacedObject" which 
you put into a variable "intf" of type "TInterfacedObject". As long as 
you use only "intf" there won't be any problem. But once you assign the 
value of "intf" to e.g. a variable "obj" of type "TObject" you'll have a 
problem, because the compiler does not know that "obj" contains a 
variable of type "TInterfacedObject" and thus the compiler can not 
create any reference counting calls. This way you would (depending on 
the situation) either create a memory leak or the interface would be 
freed before you stopped using the instance stored in "obj". So this 
situation is basically the same as we have today with the difference 
that we can now clearly say when reference counting will be applied and 
when not: if the variable is a (COM) interface then reference counting 
will be applied, otherwise it won't.

Regards,
Sven




More information about the Lazarus mailing list