COM Aggregation   «Prev  Next»

Analyze a buggy inner COM Object - Exercise

Objective: Understand why a COM object does not work when aggregated.


The following COM object does not work properly when aggregated. Why?
class IX : public IUnknown {
  virtual HRESULT __stdcall X1() = 0;

class CInnerCOMObj : public IX {
   ULONG m_refcnt;
   IUnknown *m_pOuterUnk;  
   //Holds the outer objects IUnknown

   HRESULT QueryInterface(REFIID riid, VOID **ppv) {
     return m_pOuterUnk->QueryInterface(riid, ppv);
   HRESULT AddRef() { 
     return m_pOuterUnk->AddRef(); 
   HRESULT Release() {
     return m_pOuterUnk->Release();
   HRESULT X1() { ... }
   /* Assume the IClassFactory::CreateInstance passes in the outer object's IUnknown */
   CInnerCOMObj(IUnknown *pOuter) : m_refcnt(0), m_pOuterUnk(NULL) {
     m_pOuterUnk = pOuter;

Exercise scoring

10 points: All or nothing

Exercise submission

Write or paste your answer into the text area below. When you are finished, click the Submit button to submit your answer .