COM Aggregation   «Prev  Next»

Fix a Bug in outer COM Object - Exercise

Objective: Find and fix a bug in the outer COM object.


The following code fragment is for an outer COM object, OuterCOMObj. OuterCOMObj implements interface XX. It aggregates one COM object, InnerCOMObj. OuterCOMObj holds the nondelegating IUnknown pointer to InnerCOMObj in m_pInnerUnk. Assume m_pInnerUnk is properly initialized. Find the bug.
InnerCOMObj implements interface YY.

class COuterCOMObj : public XX{
  IUnknown *m_pInnerUnk;  
  /* This holds the non-delegating IUnknown for InnerCOMObj */
  HRESULT QueryInterface(REFIID riid, 
  VOID **ppv) {
    /* See if the caller is asking for interface 
    IX0 which is implemented in the outer object  */
    if (riid == IID_XX) { 
      *ppv = (IX0 *) this;
      hr = S_OK;
    /* See if the caller wants YY which is implemented in aggregated object IO1 */
    else if (riid == IID_YY) {
      /* If so - call the non-delegating IUnknown in aggregated object InnerCOMObj*/
      hr = m_pInnerUnk->QueryInterface(riid, ppv);
      return E_NOINTERFACE;      
    if (FAILED(hr)) 
      return hr;
      ((*IUnknown ) ppv)->AddRef();
      return S_OK;

Exercise scoring

10 points either all or nothing. Write or paste your answer in the text area below. When you are finished, click Submit to submit your answer.