Help with delete of User object

R

radishcarrot

Hi, I am rather new to C++ and an invalid exception has occurs in my
system which I have absolutely no idea as to why it happens. Please
help! Thank you.

I have created a method which takes in pObject and assigned to m_pObj
which is a CGLObj pointer variable in the CGLTest class.

CGLTest::~CGLTest() //destructor
{
if(m_pObj) {
delete m_pObj;
m_pObj = NULL;
}

}

void CGLTest::SetObj(CGLObj *pObject)
{
m_pObj = pObject;
}

And the class that calls the SetObj class is as follows
CGLTesterView::~CGLTesterView()
{
if(m_pObj[0]) delete m_pObj[0];
}

void CGLTesterView::Scene()
{
CGLObj *pObj = m_pObj[nObj];
if(pObj)
{
m_model.SetObj(pObj);
m_model.Render();
}

//should I include a delete pObj here??
}

Upon closing my dialog window, an exception will occur as if when I
delete m_pObj in the CGLTest class, it deletes the CGLTesterView
m_pObj. But I am not sure as to how to solve this exception. As I am
required to delete the pointer used.

Another qns: For the CGLTest::SetObj class where I have written
m_pObj = pObject is it the same as the following 3 lines of codes??

if(m_pObj) { free(m_pObj); m_pObj = NULL; }
m_pObj = (CGL3DObj *) malloc(sizeof(CGL3DObj));
memcpy(m_pObj, pObject, sizeof(CGL3DObj)); //copied the object

Any help is greatly appreciated.. :)
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi, I am rather new to C++ and an invalid exception has occurs in my
system which I have absolutely no idea as to why it happens. Please
help! Thank you.

See the FAQ on how to post.

Short version: you should post a complete, small program that compiles
and exhibits the problem.

I have created a method which takes in pObject and assigned to m_pObj
which is a CGLObj pointer variable in the CGLTest class.

Garble, garble, garble, ..., this is only meaningful to /you/.


[snip]
m_pObj = (CGL3DObj *) malloc(sizeof(CGL3DObj));

Don't use malloc. Don't use C-style casts. Don't use raw pointers.
 
F

Frederick Gotham

posted:

if(m_pObj) {
delete m_pObj;
m_pObj = NULL;
}


You can cut that down to:


delete m_pObj;
m_pObj = 0;


(It's always okay to invoke "delete" on a null pointer.)
 
R

radishcarrot

oops I m sry....
Heres the class

CObj *m_pObj;

CTest::CTest()
{
m_pObj = NULL;
}

CTest::~CTest()
{
if(m_pObj) {
delete m_pObj;
m_pObj = NULL;
}
}

void CTest::SetObj(CObj *pObject)
{
if(m_pObj) { delete m_pObj; m_pObj = NULL; }
m_pObj = pObject;
}

The class that access the above class

CObj *m_pObj[3];
CTest m_plane;

void CViewer::CViewer
{
if(m_pObj[0]) delete m_pObj[0]; //have problem here
if(m_pObj[1]) delete m_pObj[1]; //have problem here
if(m_pObj[2]) delete m_pObj[2]; //have problem here
}

CViewer::~CViewer()
{
if(m_pObj[0]) delete m_pObj[0];
if(m_pObj[1]) delete m_pObj[1];
if(m_pObj[2]) delete m_pObj[2];
}

void CViewer::DrawS()
{

for(int nObj=0; nObj<3; nObj++)
{
CObj *pObj = m_pObj[nObj];
if(pObj) {
m_model.SetObj(pObj);
}
}
}

void CViewer::OnPaint()
{
DrawS();
}


The problem occurs when I try to exit the application and the CViewer
destructor will throw an invalid exception!

If I am not wrong at the CTest destructor, I have already deleted the
m_pObj but I am not sure. Kindly help me.

Also I wish to make a copy of the object in the SetObject class of
CTest so that the method can handle the self management of the memory.
So I did the = assignment. Did I make a fresh copy of the object or I
just point to the existing object??

Thanks for your help!
 
M

Michiel.Salters

radishcarrot said:
Heres the class

CObj *m_pObj;

CTest::CTest()
{
m_pObj = NULL;
}

CTest::~CTest()
{
if(m_pObj) {
delete m_pObj;
m_pObj = NULL;
}
}

void CTest::SetObj(CObj *pObject)
{
if(m_pObj) { delete m_pObj; m_pObj = NULL; }
m_pObj = pObject;
}

No, that doesn't look like a class. A class starts with class {
and ends with };
The class that access the above class

(again class{ }; missing)
CObj *m_pObj[3];
CTest m_plane;

void CViewer::CViewer
{
if(m_pObj[0]) delete m_pObj[0]; //have problem here
if(m_pObj[1]) delete m_pObj[1]; //have problem here
if(m_pObj[2]) delete m_pObj[2]; //have problem here
}

Wrong, in many ways. This is a *constructor*. It should not assume
old values. m_pObj[] will contain garbage. You can't just call delete
on any random pointer - the pointer had to be created using new.

The better solution is to simply overwrite the garbage. Either you
say m_pObj[0] = 0; or you say m_pObj[0] = new CObj;
In either case, you will have cleaned up m_pObj[0].

An even better solution may be to change it to CObj m_Obj[3];
so CViewer::CViewer will call the CObj consrtuctor for you.

HTH,
Michiel Salters
 
D

Daniel T.

Hi, I am rather new to C++ and an invalid exception has occurs in my
system which I have absolutely no idea as to why it happens. Please
help! Thank you.

I have created a method which takes in pObject and assigned to m_pObj
which is a CGLObj pointer variable in the CGLTest class.

CGLTest::~CGLTest() //destructor
{
if(m_pObj) {
delete m_pObj;
m_pObj = NULL;
}

}

void CGLTest::SetObj(CGLObj *pObject)
{
m_pObj = pObject;
}

And the class that calls the SetObj class is as follows
CGLTesterView::~CGLTesterView()
{
if(m_pObj[0]) delete m_pObj[0];
}

void CGLTesterView::Scene()
{
CGLObj *pObj = m_pObj[nObj];
if(pObj)
{
m_model.SetObj(pObj);
m_model.Render();
}

//should I include a delete pObj here??
}

Upon closing my dialog window, an exception will occur as if when I
delete m_pObj in the CGLTest class, it deletes the CGLTesterView
m_pObj. But I am not sure as to how to solve this exception. As I am
required to delete the pointer used.

Another qns: For the CGLTest::SetObj class where I have written
m_pObj = pObject is it the same as the following 3 lines of codes??

if(m_pObj) { free(m_pObj); m_pObj = NULL; }
m_pObj = (CGL3DObj *) malloc(sizeof(CGL3DObj));
memcpy(m_pObj, pObject, sizeof(CGL3DObj)); //copied the object

Any help is greatly appreciated.. :)

What do your copy constructors and assignment operators look like?
 
P

Paul

radishcarrot said:
oops I m sry....
Heres the class

CViewer::~CViewer()
{
if(m_pObj[0]) delete m_pObj[0];
if(m_pObj[1]) delete m_pObj[1];
if(m_pObj[2]) delete m_pObj[2];

Are you actually doing this to "create" your objects (this is from your
original post)?

m_pObj = (CGL3DObj *) malloc(sizeof(CGL3DObj));
memcpy(m_pObj, pObject, sizeof(CGL3DObj)); //copied the object

If so, stop it. Your comment is wrong -- you did not "copy an object". All
you copied was a bunch of bytes that added up to sizeof(CGl3DObj).
You do not create objects using malloc(). Hopefully you knew that and are
now either using new / new[] or using a container.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top