Re: CComObject CreateInstance Fails but only sometimes. Very bizarre behavior
Igor,
Thanks for the advice.
Re: FinalConstruct ... There is no FinalConstruct. This class
(CFormattedRegistration) is extremely simple. In fact, simple enough
that I've put it at the bottom of the message. That's why it seems
surprising that CreateInstance fails. That and the fact that it's
only sometimes and the class CFormattedRegistration does not seem to
depend on anything has me baffled. I've studied the problem a little
more and am convinced of two things.
1) I should have noted the HResult value and reported it to you. I
will do this tomorrow !
2) I should be using a simpler logging system to log to the Windows
EventLog. Right now I'm using someone else's system and that
introduces yet more unknowns. Do you have a preferred way to log to
the Application EventViewer (EventLog) ? This way I could record some
of the steps. I am suspicious that my attempts to debug the problem
are masking the actual problem.
After all, the relevant code is:
STDMETHOD(get_Registration)(IDispatch * * pVal)
{
CComObject<CFormattedRegistration>* pReg = 0;
HRESULT hr =
CComObject<CFormattedRegistration>::CreateInstance(&pReg);
if(FAILED(hr))
return Error("Failed to create object");
pReg->m_nNameLines = GetNameLineCount();
hr = pReg-
QueryInterface(IID_IDispatch,reinterpret_cast<void**>(pVal));
return hr;
}
It would make sense if it failed on QueryInterface. That behavior
WOULD change based on pVal. And maybe GetNameLineCount. But
CreateInstance should always behave the same !
As for your second suggestion that there is some sort of memory
problem... that occurred to me also and it gives me an anxiety attack
just thinking about it. I suspect that would be much more difficult
to deal with.
Thanks again for any suggestions,
Dave
CFormattedRegistration header file
#include "resource.h" // main symbols
class ATL_NO_VTABLE CFormattedRegistration :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CFormattedRegistration,
&CLSID_FormattedRegistration>,
public ISupportErrorInfo,
public IDispatchImpl<IFormattedRegistration,
&IID_IFormattedRegistration, &LIBID_REGISTRATIONFORMATSLib>
{
public:
CFormattedRegistration()
{
}
void SetLines(_bstr_t bstrLine);
void SetNameLines(long nNameLines);
DECLARE_REGISTRY_RESOURCEID(IDR_FORMATTEDREGISTRATION)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CFormattedRegistration)
COM_INTERFACE_ENTRY(IFormattedRegistration)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()
// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
// IFormattedRegistration
public:
STDMETHOD(get_Lines)(BSTR *pVal);
STDMETHOD(get_NameLineCount)(/*[out, retval]*/ long *pVal);
_bstr_t m_bstrLines;
long m_nNameLines;
};
#endif //__FORMATTEDREGISTRATION_H_
CFormattedRegistration .cpp file
// FormattedRegistration.cpp : Implementation of
CFormattedRegistration
#include "stdafx.h"
#include "RegistrationFormats.h"
#include "FormattedRegistration.h"
/////////////////////////////////////////////////////////////////////////////
// CFormattedRegistration
STDMETHODIMP CFormattedRegistration::InterfaceSupportsErrorInfo(REFIID
riid)
{
static const IID* arr[] =
{
&IID_IFormattedRegistration
};
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
STDMETHODIMP CFormattedRegistration::get_NameLineCount(long *pVal)
{
POINTER_CK(pVal);
*pVal = m_nNameLines;
return S_OK;
}
STDMETHODIMP CFormattedRegistration::get_Lines(BSTR *pVal)
{
POINTER_CK(pVal);
*pVal = m_bstrLines.copy();
return S_OK;
}
void CFormattedRegistration::SetLines(_bstr_t bstrLines)
{
m_bstrLines = bstrLines;
}
void CFormattedRegistration::SetNameLines(long nNameLines)
{
m_nNameLines = nNameLines;
}
On Jul 14, 7:49 pm, "Igor Tandetnik" <itandet...@mvps.org> wrote:
"Dave" <csharpu...@hotmail.com> wrote in message
news:1184424470.083493.124910@m37g2000prh.googlegroups.com
I inherited some legacy ATL code and am completely baffled by one
particular bug. In a certain scenarios, the following fails:
CComObject<CFormattedRegistration>* pReg = 0;
HRESULT hr =
CComObject<CFormattedRegistration>::CreateInstance(&pReg);
Really, I'm just looking for some hints to get me going on how to
start debugging this.
The return value of CreateInstance very likely comes from
CFormattedRegistration::FinalConstruct. Start debugging there.
Another possibility for error is simply failure to allocate memory.
Perhaps you have severe memory leak that manifests this way? This is a
pretty long shot though.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925