D'tor and runtime error

Hi guys.
I have a problem with my D'tor. When I implement the D'tor the I get a runtime error. If the D'tor is not implemented then the program runs just fine.
Can you compile and tell me what you think about this runtime error and how I will be able to fix it?
P.S
All of the other method works just fine. I'm adding the header files, D'tor and the main(), please tell me if you want to see something else of my code(.cpp files, etc).

Thanks in advance.


[/code]
Last edited on
this program cannot be compiled.

the usual error in the destructor is that you delete something [uninitialized] that you shouldn't
You have not implemented the copy constructor and copy assignment operator so that PassengerList can be copied correctly.

I see no implementation for a destructor, so I can't begin to guess what run-time error you're getting.

Please post the exact error message you getting.

My guess is one of the following:
1) You haven't implemented the ~PassengerList destructor and are getting a linker error indicating the linker can't find the destructor.
2) Since PassengerList is a linked list, you're trying to delete Passenger instances more than once. i.e. Consider after line 50, Passenger instances are on multiple lists.
Objects are destructed in reverse order, therefore passengerList3 is destructed first. That deletes all Passenger instances. Then passengerList2 is destructed, but the Passenger instances on passengerList2 instances have already been deleted.


Last edited on
how come they are already been deleted if passengerlist1\2\3 are differnets lists? When I make passengerlist3 I create new points and new passengers(a passenger can be in different lists at the same time)
a passenger can be in different lists at the same time
That's highly dangerous. If you delete an object from one list you need to make sure that it is removed from the other list otherwise when you try to delete an object twice your program will crash.
this is the problem in my program, and I do not understandn why it happens? How do I make sure it is deleted in all of the lists?
Can't think of idea.

This is my D'tor by the way:
1
2
3
4
5
6
7
8
9
10
11
 
PassengerList:: ~PassengerList(){ 
	Passenger* del_current=this->passengerList; 
	Passenger* next=this->passengerList; 
	while(next){
		next=next->getNextPassenger();
		delete del_current; 
		del_current=next;
	}
	delete del_current;
}
Last edited on
I do not understandn why it happens?

It happens because you have multiple pointers pointing to same passenger instnace.

When you created passengerlist3 you did a shallow copy of next (i.e. copied the pointer. You now have two passengerlists pointing to the same memory. Destructing one passenger list works fine, but when you destruct the second passengerlist, you get a run time error because you're trying to release a passenger that has already been released.

One solution is to used a std::shared_ptr.
http://www.cplusplus.com/reference/memory/shared_ptr/

BTW, please do not delete the code in your OP. It may still be relevant.
Last edited on
Topic archived. No new replies allowed.