Re: CComObject CreateInstance Fails ... Sometimes. Bizarre behavior
After you create your object, you are advised to increase its
reference count as it starts as zero. When finished (e.g. after
you call QueryInterface to obtain your interface pointer) you
shoudl Release it again. E.g.:
pReg->GetUnknown()->AddRef();
....
pReg->GetUnknown()->Release();
Try not to return from within the block, or if you do - don't
forget to Release beforehand. You can also use a variable
of type CComPtr<IUnknown> to hold your GetUnkown()
for you to the same effect.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Dave" <csharpuser@hotmail.com> wrote in message
news:1184423542.979044.240400@o11g2000prd.googlegroups.com...
Hi,
I inherited some legacy ATL code and am completely baffled by one
particular bug. In a certain scenario, the following fails:
STDMETHOD(get_Registration)(IDispatch * * pVal)
{
CComObject<CFormattedRegistration>* pReg = 0;
HRESULT hr =
CComObject<CFormattedRegistration>::CreateInstance(&pReg);
if(FAILED(hr))
return Error("Failed to create FormattedRegistration
object.",__uuidof(IRegistrationFormatter),hr);
try
{
pReg->m_bstrLines = GetFormattedLines();
}
catch(CCSErrorEvent& E)
{
return
Error(E.GetErrorMsg(),__uuidof(IRegistrationFormatter),E_FAIL);
}
pReg->m_nNameLines = GetNameLineCount();
hr = pReg-
QueryInterface(IID_IDispatch,reinterpret_cast<void**>(pVal));
return hr;
}