[Lazarus] IUnknown and reference counting
Kostas Michalopoulos
badsectoracula at gmail.com
Thu Mar 21 14:23:56 CET 2013
Yes, that is what i mean with "topmost" type: if you look at the
inheritance as a tree, TInterfacedObject is the topmost (or root, if you
think of it as an upside-down tree) of the type hierarchy. In this code
type
IBlah = interface ... end;
TBlah = class(TInterfacedObject, IBlah) ... end;
TBlah is an IBlah, so i'd expect it to behave like IBlah.
On Thu, Mar 21, 2013 at 2:10 PM, Sven Barth <pascaldragon at googlemail.com>wrote:
> Am 21.03.2013 13:55, schrieb Kostas Michalopoulos:
>
> I haven't used (COM) interfaces so far since i didn't found any use for
>> them in my code, but i didn't knew about their reference counting
>> properties. That could save a lot of headaches i have with my 3D world
>> editor's lightmap generation (currently there is some wrong memory
>> deallocation somewhere that crashes the editor in partial lightmap
>> recalculations). I plan to redesign it at some point soon and i was
>> thinking how to handle this. Since there is native refcounting support in
>> FPC it makes things much easier.
>>
>> However i did a small test and i noticed something that, to me (as
>> someone who hasn't used COM at all) looks a bit weird:
>>
>> If i do a declaration like
>>
>> type
>> IResource = interface end;
>> TResource = class(TInterfacedObject, IResource) ... stuff ... end;
>>
>> TSomething = class
>> ...
>> Resources: array of TResource;
>> ...
>> end;
>>
>> then reference counting doesn't work. However if i change Resources to
>>
>> Resources: array of IResource;
>>
>> then it works. It seems that the compiler checks only the "topmost" type
>> and doesn't check if TResource implements the IResource and thus doesn't do
>> any reference counting. Is this a bug or it is supposed to work this way?
>>
>> If the latter, is there a way to make the compiler do reference counting
>> on a variable that has a class type which implements an interface instead
>> of a type that is an interface itself? Or is there any other form of
>> reference counted classes?
>>
>> The compiler does not check for any topmost type. It uses reference
> counting only for variables that are of an interface type not for a class
> type.
>
> E.g.:
>
> === example begin ===
>
> var
> i: IInterface;
> t: TInterfacedObject;
> begin
> t := TInterfacedObject.Create;
> t.Free;
> // no reference counting is done here
>
> i := TInterfacedObject.Create;
> i := Nil;
> // reference counting is done here
> end;
>
> === example end ===
>
> Regards,
> Sven
>
>
> --
> ______________________________**_________________
> Lazarus mailing list
> Lazarus at lists.lazarus.**freepascal.org<Lazarus at lists.lazarus.freepascal.org>
> http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20130321/40916f74/attachment-0003.html>
More information about the Lazarus
mailing list