benbenben
February 21st, 2003, 05:49 AM
Hi,
I have two classes CA and CB, CA stores the object pointer of CB.
When Hello() is called object pointer of CB is stored in m_pObjB.
When scope of Hello() ceases, destructor of CA is invoked, which deletes m_pObjB. But this results in pObjB getting invalid.
Now if m_pObjB is not deleted in destructor of CA, there will be memory leak.
All I need is to store *CB in CA. But the *CB comes from outside CA(ie via the constructor).
I would be glad if someone can throw light on this.
Thanks in Advance
Regards
Ben
Following is the code snippet:
class CB;
class CA
{
public:
CA(CB* pObjB)
{
m_pObjB = pObjB;
}
CA();
virtual ~CA()
{
if(m_pObjB)
{
delete m_pObjB;
m_pObjB = NULL;
}
}
private:
CB* m_pObjB;
};
class CB
{
public:
void Hello()
{
CA aObj(this);
}
CB();
virtual ~CB();
};
int main()
{
CB* pObjB = NULL;
pObjB = new CB;
pObjB->Hello();
delete pObjB;
return 1;
}
I have two classes CA and CB, CA stores the object pointer of CB.
When Hello() is called object pointer of CB is stored in m_pObjB.
When scope of Hello() ceases, destructor of CA is invoked, which deletes m_pObjB. But this results in pObjB getting invalid.
Now if m_pObjB is not deleted in destructor of CA, there will be memory leak.
All I need is to store *CB in CA. But the *CB comes from outside CA(ie via the constructor).
I would be glad if someone can throw light on this.
Thanks in Advance
Regards
Ben
Following is the code snippet:
class CB;
class CA
{
public:
CA(CB* pObjB)
{
m_pObjB = pObjB;
}
CA();
virtual ~CA()
{
if(m_pObjB)
{
delete m_pObjB;
m_pObjB = NULL;
}
}
private:
CB* m_pObjB;
};
class CB
{
public:
void Hello()
{
CA aObj(this);
}
CB();
virtual ~CB();
};
int main()
{
CB* pObjB = NULL;
pObjB = new CB;
pObjB->Hello();
delete pObjB;
return 1;
}