[Lazarus] IUnknown and reference counting

Kostas Michalopoulos badsectoracula at gmail.com
Thu Mar 21 13:55:25 CET 2013


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?


On Thu, Mar 21, 2013 at 12:45 PM, Michael Schnell <mschnell at lumino.de>wrote:

> On 03/21/2013 12:30 PM, Graeme Geldenhuys wrote:
>
>> On 2013-03-21 08:33, Michael Schnell wrote:
>>
>>> parameters to the IDE when designing the program ? Based on this, COM /
>>> CORBA / ORB might be described as specializations of the concept.
>>>
>> I'm not sure I understand your question.
>>
>> COM is Component Object Model and DCOM is Distributed Component Object
>> Model, developed by Microsoft. This only works on the Windows platform.
>> That is how the do ActiveX, and how Delphi does multi-tier support with
>> MIDAS (using DCOM).
>>
> That is exactly why I think the *language-concept* (here a thingy that
> provides reference counting, auto-creation and auto-deletion of an instance
> etc of a classes instance) should be described independently of COM, DCOM,
> Axtive X, MIDAS and such things that are not related to the language
> itself, while the language concept/syntax - being useful in itself, anyway,
> can *additionally* be used to allow the programmer to handle  COM, DCOM,
> Axtive X, MIDAS and such things.
>
>
>  In terms of Free Pascal and Interfaces support. COM style interfaces
>> simply means they are _compatible_ with Microsoft COM, and are reference
>> counted. CORBA style interfaces are _compatible_ with how interfaces are
>> implemented in the CORBA standard, and there they are not reference
>> counted.
>>
> Yep. But IMHO calling them something like "reference counting interfaces"
> and  "not reference counting interfaces" instead of "COM style interfaces"
> and "CORBA style interfaces" would be a lot more appropriate, as they are
> defined and usable for completely other stuff as well. In fact the term
> "interface" is rather misleading (as they are defined and usable for
> completely other stuff as well), but this of course is a Delphi heritage.
>
>
>  For more information on COM, DCOM, CORBA and XPCOM technologies, search
>> WikiPedia.
>>
>>  Not necessary because (I already know) and *here* I am interested in the
> language concepts and not in the environment of the IDE or the program to
> be created.
>
> -Michael
>
>
> --
> ______________________________**_________________
> 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/04f6ce9c/attachment-0003.html>


More information about the Lazarus mailing list