0

I have a situation where I need to pass a CComPtr<IXmlReader> to a function by reference. Does the called parameter takes the ownership from the callee parameter (or) the reference count is increased?

void foo( CComPtr<IXmlReader> & pReader )
{
  // There is no reassignment of the CComPtr.
  // Just call the IXmlReader methods.
}

CComPtr<IXmlReader> pReader;
foo( pReader );

// Is pReader still valid after the function return ?

Thanks.

Mahesh
  • 34,573
  • 20
  • 89
  • 115

2 Answers2

1

If there is no reassignment, why is it a reference parameter?

Regardless, there is no change in the reference count. Yes, the CComPtr is still valid after returning.

dsharlet
  • 1,036
  • 1
  • 8
  • 15
  • Perhaps he means to pass by const reference for performance reasons. – 01100110 Apr 11 '12 at 11:37
  • If there is no need for reference counting (changing the value of the pointer) in the foo function, and you are worried about reference counting overhead, I would just make the parameter a simple pointer (non-CComPtr wrapped pointer). – dsharlet Apr 11 '12 at 16:59
0

No reference counting affected by the call itself, it is only inner function manipulation with the pointer which might be adding or releasing number of references to the object.

// Is pReader still valid after the function return ?

Yes, unless the function re-assigned the value, setting it to NULL or putting in there a new value.

Even if the argument in input-only, there is a programmer's bonus in passing arguments this way: (1) you are completely staying within domain of smart pointers and you don't need to worry a lot about proper reference counting (2) you have assertion failure on e.g. trying to -> an uninitialized pointer (3) release build optimizations will/might be generating nearly as fast code as if you used raw pointer instead.

Roman R.
  • 68,205
  • 6
  • 94
  • 158