COM Smart pointer doesn't keep its reference
COM Smart pointer doesn't keep its reference
I am creating a wrapper around a COM object that consists of objects in
a tree structure. For example a collection class (col1) contains obj1.
Obj1 has member variables including another collection (col2) which
contains obj2, etc.
Each collection implements a common interface so that the getting next
and previous can be abstracted to a base wrapper class. In the
collection classes I have variable that stores the index of the current
object and I use the function getObject1, getObject2 etc to get the
respective object.
I basically use the same pattern of code for all the objects even
though they don't have a common base class (they perhaps could have
done but that is another story). Each COM object is wrapped by my
classes. Each class stores as a member variable a pointer to the COM
interface (not to the Smart pointer - is this perhaps where the problem
lies). My code for the creation of one of the object is shown below.
I am calling a function to return a CSV of the properties of the object
CString CCol2::getRow(void)
{
CString sRow;
CObj2* pObj2 = getObj2();
//At this point the wrapped COM object m_pCOMObj2 (not see here
//m_pCOMObj2 is the wrapped COM object. I made it temporarily
public so I could test this.
//it crashes here. m_pCOMObj2 is not longer a valid pointer.
CString sTemp = pObj2->m_pCOMObj2->Fields[1].bstrval;
sRow = pObj2->getRow();
delete pObj2;
return sRow;
}
// Returns the current member
CObj2* CCol2::getObj2(void)
{
COMNamespace::_CCOMCol2Ptr pCOMCol2;
COMNamespace::ICommonObjectPtr pDataObjPtr;
COMNamespace::_CCOMObj2Ptr pCOMObjPtr;
HRESULT hr;
hr = pCOMCol2.CreateInstance(__uuidof(COMNamespace::CCOMCol2));
hr = pCOMObjPtr.CreateInstance(__uuidof(COMNamespace::CCOMObj2));
pDataObjPtr = m_pCollection->Item[m_lCurrent];
hr = pDataObjPtr.QueryInterface(__uuidof(COMNamespace::_CCOMObj2),
(void**) &pCOMObjPtr);
CObj2* pObj2 = new CObj2(pCOMObjPtr,m_pSessionContext,m_sParentID);
//m_pCOMObj2 is the wrapped COM object. I made it temporarily
public so I could test this and it is a valid pointer
CString sTemp = pObj2->m_pCOMObj2->Fields[1].bstrval;
return pObj2;
}
I hope that this is clear. What is really strange is that if I put sRow
= pObj2->getRow() at the end of the function getObj2() it does not
crash
later! Also this does not crash with Col1 and Obj1, and Col3 and Obj3
despite the fact that I use the same patter of code.