How to destruct the CComPtr when it is made as Member Variable for a Class?
Following is the piece of Code I have written and m_piControl is the member variable CComPtr . I have assigned the value of m_piControl to rpControl and rpControl used further.
HRESULT CSession::GetInterface(OUT CComPtr<IControl>& rpControl )
{
if (m_piConSIControl == NULL)
{
CComPtr<IDispatch> pConDM;
HRESULT hResult = GetMaintenance( &pConDM );
if( FAILED( hResult ) )
return hResult;
CComQIPtr<IMaintenance> pMaintenance( pConDM );
if( !pMaintenance )
return E_NOINTERFACE;
hResult = pMaintenance->GetControl( &m_piControl );
if( FAILED( hResult ) )
return hResult;
}
rpControl = m_piControl;
return S_OK;
}
On Assignment, m_piControl refcount increase. But the release for m_piControl not getting called. Should I call it explicitly?
Release of m_piControl's COM interface pointer will happen when the [member] variable is destroyed, which in turn happens with destruction of the owner class instance. This does happen "automatically" and reference count is properly managed. That is, you don't need to release m_piControl's value explicitly.
If you for whatever reason still want to release the interface pointer explicitly, you can at any time (provided that you comply with COM threading, of course) call m_piControl.Release();. For example, this might happen before you return S_OK in your code snippet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With