what keeps a COM object alive?

Sean Cavanaugh WorksOnMyMachine at gmail.com
Wed Jun 12 07:17:58 PDT 2013


On 6/11/2013 10:38 PM, finalpatch wrote:
> A typical COM server would create a new object (derived from IUnknown),
> return it to the caller (potentially written in other languages).
> Because the object pointer now resides outside of D's managed heap, does
> that mean the object will be destroyed when the GC runs? A normal COM
> object written in C++ relies on reference counting to manage its life
> cycle but in D the ref counting seems not serving any purpose. The
> AddRef()/Release() of std.c.windows.com.ComObject maintains a ref count,
> but doesn't really use it for any thing. There's a comment in Release()
> says "let the GC reap it", but how does the GC know the object is okay
> to destroy?

COM is by definition ref counted.  In D you generally need to store a 
COM pointer in a struct and maintain the refcount with that struct 
(increment on copy, decrement in destructor, etc).  Its not too hard, as 
'alias this' usage can wrap the pointer's methods easily enough.




More information about the Digitalmars-d-learn mailing list