CComObject CreateInstance Fails but only sometimes. Very bizarre behavior
Hi,
I inherited some legacy ATL code and am completely baffled by one
particular bug. In a certain scenarios, the following fails:
STDMETHOD(get_Registration)(IDispatch * * pVal)
{
CComObject<CFormattedRegistration>* pReg = 0;
HRESULT hr =
CComObject<CFormattedRegistration>::CreateInstance(&pReg);
if(FAILED(hr))
{
// also try write to Event LOG
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;
}
It fails (sometimes) on the line "CreateInstance(...)" and goes into
the "if(FAILED(hr).." block.
This get_Registration method is itself a method in a com object
(Car). As I said,
it fails under certain scenarios depending on how the Car was created
(number of wheels,
transmission, etc), and I can definately give more details if
necessary. But what's immediately
puzzling me is this:
The line ...CreateInstance... would seem to be stand alone and not
have any dependencies on the outer object.
If the code gets this far, it seems like it should be always working
or always failing. Am I missing something basic here?
I've read horrible things about messed up vtables and memory
conflicts. I'm wondering if it's something like that here.
Really, I'm just looking for some hints to get me going on how to
start debugging this.
Finally, one other piece of very confusing data. In a desparate
attempt to gather more inforation, I logged to the Event Log
inside the if(FAILED(hr)) block. Well, the error message made it into
the log alright but now the block is hit in ALL CASES. Why would
putting a statement (any statement) inside the "if(FAILED(hr))" block
cause a line before that (CreateInstance) to fail?
Surely, this violates some sort of causality or time-travel
constraint ? :)
As you can see, I'm a newbie at this, and I appreciate all hints,
links, tutorials, and flames.
Thanks,
Dave