COM Aggregation   «Prev  Next»

COM Aggregation - Exercise

Objective: Identify the aggregation bugs.

Instructions

The following pseudo-code does not work properly when aggregated. Identify the two bugs causing the problems.
class IA : public IUnknown {
  virtual HRESULT __stdcall A1() = 0;
};

class IB : public IUnknown {
  virtual HRESULT __stdcall B1() = 0;
};

class INonDelegatingIUnknown {   
  virtual HRESULT __stdcall objQueryInterface(REFIID riid, VOID **ppv) = 0;
  virtual ULONG __stdcall objAddRef() = 0;
  virtual ULONG __stdcall objRelease() = 0;
};

class CCOMObj : INonDelegatingIUnknown, IA, IB {
 private:
   ULONG m_refcnt;
   IUnknown *m_pIOuterIUnknown;
public:
   //INonDelegatingIUnknown methods
   HRESULT objQueryInterface(REFIID riid, VOID **ppv) { ... }  
   ULONG objAddRef() { ... };
   ULONG objRelease() { ... }
   //IUnknown methods for IA and IB
   HRESULT QueryInterface(REFIID riid, VOID **ppv) {
     return m_pIOuterIUnknown->QueryInterface(riid, ppv);
   }
   ULONG AddRef() {
     return ++m_refcnt;
   }
   ULONG Release() {
     if (--m_refcnt == 0) {
       delete this;
       return 0;
     }
     return m_refcnt;
   }
   //IA methods
   HRESULT A1() { ... }
   //IB methods
   HRESULT B1() { ... }
   /*Assume that the outer object's IUnknown pointer is passed into the constructor */
   CCOMObj(IUnknown *pOuter) : m_refcnt(0),m_pIOuterIUnknown(NULL) {
     m_pIOuterIUnknown = pOuter;
   }
};

Exercise scoring

10 points per bug; 20 points total for the exercise

Exercise submission

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