Interfacing with XPCOM

John C johnch_atms at
Tue Dec 2 04:54:44 PST 2008

Walter Bright Wrote:

> John Reimer wrote:
> > In D (windows) it would look like so (assuming you have done away with 
> > the compilers internal detection of COM interfaces):
> > 
> > extern(C++) interface IUnknown:
> > {
> >     extern(Windows):
> >         int QueryInterface(nsIID* uuid, void **result);
> >         int AddRef();
> >         int Release();
> > }
> > 
> > I bet it would work fine and would be a good way to avoid the 
> > special-cased interfaces providing COM support in D.  Instead we've 
> > special-cased for C++, which is, at least, a more general case. ;-)
> There is one other problem. COM objects are not collected by the garbage 
> collector because they are reference counted. Deriving from IUnknown 
> means its a COM object which sets a flag in the ClassInfo, directing the 
> garbage collector to not collect it. In fact, COM objects are allocated 
> using malloc(). IUnknown.Release() uses free() to deallocate them.

But IUnknown.Release does no such thing. If you're talking about the implementation in, then all it does is decrement the reference count. It has a comment saying "let the GC reap it" - which is incorrect, since the GC doesn't collect COM objects (as you say above). So anyone deriving from ComObject is leaking memory.

John C.

More information about the Digitalmars-d mailing list