473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Help with delete of User object

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
6 1460
* 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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cheezebeetle | last post by:
ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they press "OK" they call this ASPX function, when...
2
by: I am Sam | last post by:
Ok I set up a button column to delete an individual record. Nothing happens when I test it. Someone please help understand why. Below is the associated coding: DataGrid control: ...
3
by: Joe | last post by:
Hi, I have written a webpage that allows a user to delete files in asp.net with I am having a small problem. To access this page a user has to login via login.aspx page. After successful...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
4
by: Jeff User | last post by:
Hi I tryed to solve this problem over in the framework.asp group, but still am having trouble. Hope someone here can help. using .net 1.1, VS 2003 and C# I have an asp.DataGrid control with a...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.