Click to See Complete Forum and Search --> : Memory Leak-Help Needed


Kohinoor24
February 17th, 2003, 04:01 AM
I have located a memory leak in my program.It shoots up.I dont know why?.This is the implementation Iam doing & it is coming into picture only under Linux.

I have a class as follows:

class C_AfpDrawer:public I_Drawer
{
public: // c_Ptx is an object Iam creating
void StoreTablePtx(C_Ptx& Object);
C_Ptx * GetTablePtxObject();

//Destructor which Iam calling to delete the objects created.
C_AfpDrawer::~C_AfpDrawer()
{
delete m_Mdobject;
delete m_Idmobject;
delete m_Textptxobject;
delete m_Gobject;
delete m_Tableptxobject;

}

private:
C_Idm * m_Idmobject;
C_Ptx * m_Textptxobject;
//Iam showing only this pointer as an example.Iam doing the same with other pointers as well.
C_Ptx * m_Tableptxobject;
C_MixedData* m_Mdobject;
C_Goca *m_Gobject;

}


The implementation is as follows:

void C_AfpDrawer::StoreTablePtx(C_Ptx& Object)
{
m_Tableptxobject = &Object;
}

C_Ptx * C_AfpDrawer::GetTablePtxObject()
{
return m_Tableptxobject;
}

In another class say,C_Afppainter
C_Afppainter has a

C_AfpDrawer & m_drawer;//reference

void C_Afppainter::Func()

{
// This Func() function is called a lot of times & I need to create a new object each time it is called.
// So Iam doing as follows.
if (m_drawer.GetTablePtxObject())
delete (m_drawer.GetTablePtxObject());
C_Ptx *ptx = new C_Ptx();
m_drawer.StoreTablePtx(*ptx);
// So the last time Func() is called ,the object created Iam deleting in the C_AfpDrawer Destructor.
}

Is there a memory leak in what Iam doing....Please help..

Paul McKenzie
February 17th, 2003, 07:48 AM
See my detailed response in VC++ forum. Also, please don't cross-post.

Basically, your class design uses dynamically allocated pointers, and according to the code you posted, there is no handling in your class to handle assignment, copy construction, etc. with respect to the pointers.

A proper C++ class is written so that assignment and copying result in safe operations, both in memory management and object creation / destruction. If this can't be satisfied, the copy constructor and assignment should be made private so as to enforce that the object cannot be assigned to or copied.

Therefore I'm not surprised you would get memory leaks or program crashes due to the nature of your class design. Please post enough of your class interface and a main() stub that demonstrates the error so that others can compile and run your code.

Regards,

Paul McKenzie