473,387 Members | 1,834 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,387 software developers and data experts.

Check to see if object exists before attempting to delete it

I have a composition object that is written in C++. The container class deletes the dynamic object in its destructor. However, if someone creates the object in the main and passes it to the container class, they will probably try to delete the dynamic object in their code. How do I test to see if the object exists before I delete it? The code below crashes since the dynamic Engine was deleted in the main. However, I need to clean up the memory in the class in case the Vehicle is created with the default constructor and the main() never uses the "new" keyword. Thanks!

int main( ){
// create composition object
Engine * engine = new Engine( );
Vehicle vehicle( engine );

// clean the memory
delete engine;
}

class Vehicle{
public:
// constructors
Vehicle( ){
engine = new Engine( );
}
Vehicle( Engine * engine ){
this->engine = engine;
}

// destructor
~Vehicle( ){
delete engine;
}
private:
Engine * engine;
};
Oct 10 '08 #1
4 7643
Ganon11
3,652 Expert 2GB
No matter whether the default constructor is called or you use the 1 argument constructor, you are still assigning to an Engine* in Vehicle. Thus, you should be deleting engine only in Vehicle. Do not delete it in main() - when your Vehicle goes out of scope and the destructor is called, it will delete engine for you.
Oct 10 '08 #2
boxfish
469 Expert 256MB
How about setting engine = NULL wherever you delete it and then only deleting it if engine != NULL?
Oct 10 '08 #3
Banfa
9,065 Expert Mod 8TB
How about setting engine = NULL wherever you delete it and then only deleting it if engine != NULL?
That wont work, setting the local variable in main to NULL will not effect the value of the engine member in Vehicle so the Vehicle destructor would still try to delete it and crash.

Ganon's idea is right, once you have passed ownership of the engine to the Vehicle it is up to the Vehicle to delete it.

Of course it might be better to just let Vehicle create its own engine all the time, then all the memory handling is nicely encapsulated by Vehicle. In the constructor that takes an engine * you would use that to initialise the engine created by Vehicle.

Another option would be to use handles rather than pointers.
Oct 10 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Another option would be to use handles rather than pointers.
In fact, this is the solution.

Read this: http://bytes.com/forum/thread651599.html.
Oct 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Rick | last post by:
Hi, Does deleting an object more than one times incur undefined behavior? I think it doesn't but just making sure... thanks Rick
5
by: jez123456 | last post by:
Hi, I’ve written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete C:\test1.mdb and rename C:\temp.mdb as...
6
by: Dino Buljubasic | last post by:
My application creates some temporary files that are deleted when my application terminates. However, if a temp file is open, it will not be deleted and application will crash. How can I...
9
by: Dino Buljubasic | last post by:
If I want to delete a file I can call File.Delete(filePath) but what happens if I am trying to delete the file that is open??? In java you would do someting like int status =...
1
by: aaa | last post by:
What is the most efficient way to do a check to see if a webservice exists before attempting to execute. I was thinking of just creating an HTTP object then initiating a request to see if i get a...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
12
by: Joe | last post by:
I might be overworked so please excuse this stupid question... Say I do the following: DataTable table = new DataTable(); myDataAdaptor.Fill(table); dataGrid1.DataSource = table;
3
by: pollygw | last post by:
I have a page that dynamically adds rows to a table and the user can also delete any of the rows in no specific order. When the form is submitted I need to do some validation. I can't loop through...
14
by: Mark | last post by:
Hi, I would like to check if my object has been deleted or not, but my program keeps crashing. Here's the simplified code (in infinite loop) delete tetromino; //if(tetromino==NULL)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.