Re: CreateObject confused
Ok I think I have found what I'm doing wrong that is causing the leak.
First off I kept doing experiments, I verified that the uncommented code
writes exactly the same contents to file as the embedded call of
// ar.SerializeClass(RUNTIME_CLASS(CMapStringToString));
// ExpMap1.Serialize(ar);
if (ar.IsStoring())
{
ar << &ExpMap1; // the & works here.
}
else
{ // However I still had to use a ptr here, a & operator flags an err.
ar >> pExpMap1; // &ExpMap1 here flags an error C2679
}
//---------------------
And further the above uncommented method does read the data back
in and can be accessed from the CmStS calls. In fact as far as the file
goes both coding methods write and read the exact same file contents.
BUT the ptr passing method leaks memory.
And it appears to be my fault in that I did not search the docs for enough
details. I did find this one example in my docs that is pertinant to the problem.
Evidently if you create your object as an embedded member of the class then
you must call it's serialize method with an embedded call as in
SerializableClassObject.Serialize(ar);
whereas objects allocated inside constructors or on the heap
can be serialized with the CObject* ptr passed method.
Obviously by my passing a ptr to an embedded member was throwing
a wrench into the ReadObject method.
---------------------------------
The following example illustrates the cases:
class CMyObject : public CObject
{
// ...Member functions
public:
CMyObject() { }
virtual void Serialize( CArchive& ar ) { }
// Implementation
protected:
DECLARE_SERIAL( CMyObject )
};
class COtherObject : public CObject
{
// ...Member functions
public:
COtherObject() { }
virtual void Serialize( CArchive& ar ) { }
// Implementation
protected:
DECLARE_SERIAL( COtherObject )
};
class CCompoundObject : public CObject
{
// ...Member functions
public:
CCompoundObject();
virtual void Serialize( CArchive& ar );
// Implementation
protected:
CMyObject m_myob; // Embedded object //<-This is my scenario !
// !!! *****************************************
// Only difference is my CmStS Obj is in MyDocClass
// !!!
COtherObject* m_pOther; // Object allocated in constructor
CObject* m_pObDyn; // Dynamically allocated object
//..Other member data and implementation
DECLARE_SERIAL( CCompoundObject )
};
IMPLEMENT_SERIAL(CMyObject,CObject,1)
IMPLEMENT_SERIAL(COtherObject,CObject,1)
IMPLEMENT_SERIAL(CCompoundObject,CObject,1)
CCompoundObject::CCompoundObject()
{
m_pOther = new COtherObject; // Exact type known and object already
//allocated.
m_pObDyn = NULL; // Will be allocated in another member function
// if needed, could be a derived class object.
}
void CCompoundObject::Serialize( CArchive& ar )
{
CObject::Serialize( ar ); // Always call base class Serialize.
// !!! *****************************************
m_myob.Serialize( ar ); // Call Serialize on embedded member.
m_pOther->Serialize( ar ); // Call Serialize on objects of known exact type.
// Serialize dynamic members and other raw data
if ( ar.IsStoring() )
{
ar << m_pObDyn;
// Store other members
}
else
{
ar >> m_pObDyn; // Polymorphic reconstruction of persistent
// object
//load other members
}
}
// end example