LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2009, 11:58 AM   #1
Fredstar
Member
 
Registered: Jul 2004
Location: Rochester, NY
Distribution: Fedora9::FreeBSD7.1
Posts: 296

Rep: Reputation: 30
Deconstructor gives Assertion error?


Hi All,

Throughout this course this forum has been very helpful and i want to thank you all for that in advance.

I'm having an issue w/ my deconstructor.

Example
Code:
//Class Automobile
public:
 int getMake();
 int getModel();
 void setMake( int someValue);
 void setModel(int someValue);
 Automobile();
 Automobile& operator=( Automobile &aCar );

private:
 int make;
 int model
And a class Dealership that uses Automobile

Code:
//Dealership
public:
 Dealership();
 ~Dealership();

private:
 Automobile *carList;


//some methods
Dealership::Dealership()
{ 
 carList = new Automobile[20];
}

Dealership::~Dealership()
{ 
 delete [] carList;
}

//Some other methods
Now, my question is i though that you had to call delete on objects created on the heap using "new", if so why would this fail?

Thanks,
Freddy
 
Old 05-06-2009, 12:24 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You're correct: if you allocate an array like this:

"carList = new Automobile[20];"

... then you should deallocate it like this:

"delete[] carList;"

So far, so good.

But...

Q: What is the exact error?

Q: is it caused directly by your "delete[] carList" statement .. or caused by "something else"?

Q: Do you pass out references to your "carList[]" array or to any of its elements? If so, is there a possibility some client holding that reference has a "dangling pointer"? Or perhaps inadvertantly did their own "delete"?

Q: Do either "Automobile" or "Dealership" use inheritence and, if so, have you declared any needed virtual destructors correctly?

Etc etc

Strong suggestion:
1. Step through the code under the debugger carefully
2. Make sure all "new's" and "deletes" are strictly controlled (ideally, each occurs in exactly *one* place).

'Hope that helps ..PSM

PS:
C++ calls them "destructors" (not "deconstructor").
 
Old 05-06-2009, 12:25 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
"why would this fail?"
Ownership, more specifically what instance owns the array or automobiles?
You personally do not define the copy constructor or the assignment operator but these are some of the "special" functions which the compiler auto generates for you.
Code:
{
 Dealership d1;
{
 Dealership d2(d1);
}//d2 goes out of scope and deletes carList
}//d1 goes out of scope and boom it deletes a pointer who's value is not null and has already been deleted.
//double deletion.
You should really be using std::vector in which case your assignment operator could look like the following

Code:
Dealership& Dealership::operator =(Dealership const& rhs)
{ 
  if(this != &rhs)
  {
    carList = rhs.carList;
  }
  return *this;
}
This is not the only method and for instance you could make the parameter be passed by value and swap the vectors.
Code:
Dealership& Dealership::operator =(Dealership rhs)
{ 
  if(this != &rhs)
  {
    carList.swap(rhs.carList);
  }
  return *this;
}

Or even not supply the operator and use a std::vector as the structure has the operator defined and will do the correct thing. For a general rule of thumb use the rule of three.

Last edited by dmail; 05-06-2009 at 12:48 PM. Reason: added wiki link
 
Old 05-06-2009, 10:13 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

DMail is absolutely correct - but I still encourage you to step through it under the debugger. A nice, concrete example like this one will probably better help you understand what's happening ... and why "the rule of three" is so important.

IMHO .. PSM
 
Old 05-07-2009, 05:46 AM   #5
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
Automatic Memory Management is one of the major reasons I moved to Java as my preferred programming language!


Debian

Last edited by amysaraantony; 05-15-2009 at 08:16 PM.
 
Old 05-07-2009, 01:47 PM   #6
Fredstar
Member
 
Registered: Jul 2004
Location: Rochester, NY
Distribution: Fedora9::FreeBSD7.1
Posts: 296

Original Poster
Rep: Reputation: 30
Thanks guys, sorry i didn't have time to post back until now.

@dmail
Ya, i wish i could just use vectors. I don't know why but our professor advised us we cant use vectors on this project. I totally agree with you there.

However, i really must thank you because your post really pointed out the error in my logic of how i was thinking about my objects. I was totally looking into Automobile and overloading the assignment operator and copy constructor. I didn't even stop to realize that in my driver class i am passing around the dealership class quite often.

When i created the copy constructor it resolved my Assertion error.

@paulSm4
Ya, i was sure it was my "destructors" that was causing the error. every time i removed the delete [] carList i didn't receive the assertion error.

Big kudos to both and as always thanks for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
openldap assertion error -- ldap_parse_result: Assertion `r != ((void *)0)' failed nottings Linux - Server 1 02-19-2009 12:20 AM
Kernel error: Assertion failure in dx_probe() scm86 Linux - Kernel 0 10-09-2006 09:08 PM
Get Assertion Error when installing FC3 st95accord Fedora - Installation 1 04-02-2005 12:22 AM
K3b Quit Working--Assertion Failed Error mooreted Linux - Software 3 04-17-2004 12:54 PM
Compiling Java Assertion error CragStar Linux From Scratch 13 01-21-2003 01:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration