473,772 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::~CGLTe st() //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(C GL3DObj));
memcpy(m_pObj, pObject, sizeof(CGL3DObj )); //copied the object

Any help is greatly appreciated.. :)

Jun 28 '06 #1
6 1475
* ra**********@ya hoo.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(C GL3DObj));


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(C Obj *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::CViewe r
{
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::~CView er()
{
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::OnPain t()
{
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(C Obj *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::CViewe r
{
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::CViewe r will call the CObj consrtuctor for you.

HTH,
Michiel Salters

Jun 28 '06 #5
In article <11************ **********@m73g 2000cwd.googleg roups.com>,
ra**********@ya hoo.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::~CGLTe st() //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(C GL3DObj));
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

"radishcarr ot" <ra**********@y ahoo.com> wrote in message
news:11******** *************@j 72g2000cwa.goog legroups.com...
oops I m sry....
Heres the class

CViewer::~CView er()
{
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(C GL3DObj));
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
5077
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 they press "Cancel" they call another ASPX function. My code now is: System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf) System.Web.HttpContext.Current.Response.Write("if (confirm('Are you sure you want to...
2
1164
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: <asp:datagrid id="clubdg" DataKeyField="UserID" runat="server" GridLines="Vertical" CellPadding="2" BorderWidth="1px" BorderColor="#CCCCFF" CssClass="txt" AutoGenerateColumns="False" OnCancelCommand="Cancel_Click"
3
1914
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 login, user is directed to a page called view.aspx which shows the user the files in a directory and allows them to delete the files. This page has a data grid having 4 columns - delete button, File Name, Last Write Time and File Size. When a user...
3
2238
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 some reason I keep getting the error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
6
4997
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
5
2725
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 my best to convert it, but somehow the app does not work. I am also not getting any errors when I complie thus, letting me know I am on the write track. Basically what I want to do is edit,add,delete, and update an XML file in a DataGrid. Below...
4
2138
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 Delete button on the end of each row. I am unable to gain access to the event when the button is clicked. I don't fully understand how the click gets connected to the C# code,
6
2255
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 contains a pointer to int values as a filed. In the definition (implementation) of constructor I use this pointer to create table of int values with the new operator. The number of elements of the table is provided by the user during execution of the...
2
2985
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 standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it...
2
3765
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 when i do the following. if you add a contact with up to 13 fields it will be stored in a data structure. i have a tabbed pane that will show six of the 13 fields for that contact. when you double click the contact i want it to pop up and show all 13...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.