By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Help with delete of User object

P: n/a
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.. :)

Jun 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* ra**********@yahoo.com:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 28 '06 #2

P: n/a
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.)
--

Frederick Gotham
Jun 28 '06 #3

P: n/a
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!

Jun 28 '06 #4

P: n/a
radishcarrot wrote:
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

Jun 28 '06 #5

P: n/a
In article <11**********************@m73g2000cwd.googlegroups .com>,
ra**********@yahoo.com wrote:
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?
Jun 28 '06 #6

P: n/a

"radishcarrot" <ra**********@yahoo.com> wrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
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
Jun 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.