destructor and assignment operator

This is a discussion on destructor and assignment operator within the C++ Programming forums, part of the General Programming Boards category; I have the following destructor and assignment operator Code: //destructor Vehicle::~Vehicle( ) { if (s != NULL){ std::cout << "destructing ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    destructor and assignment operator

    I have the following destructor and assignment operator

    Code:
    //destructor
    Vehicle::~Vehicle( )
    {
     if (s != NULL){
      	std::cout << "destructing " << s << std::endl;
    	delete s;
     }
     else
    	std::cout << "not destructing " << std::endl;	
    }
    
    
    // assignment operator
    Vehicle& Vehicle::operator=( const Vehicle& other )
    {
    	if( &other != this )
    	{
              s = other.s;
    	   L_ = other.L_; 
              deltaS = other.deltaS;
    	}
    	
    	return *this;
    }
    problem is it gives me an error of double free, how is this possible?? if s is already deleted previously s would be null and it wouldn't be deleted again... why is this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    You are just copying a pointer in your copy assignment operator. So, if the source object is destroyed, the object that its s points to is destroyed. It so happens that the destination object's s points to the same object, and its destructor tries to destroy that object again.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    That's it.
    You have to choose between copying the thing pointed at, or using a smart pointer to the item (or similar such behaviour), or using move semantics.
    It's also usually a bad idea to test for self-assignment. See GotW for why
    You also don't need to test for NULL prior to deletion.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    You are just copying a pointer in your copy assignment operator. So, if the source object is destroyed, the object that its s points to is destroyed. It so happens that the destination object's s points to the same object, and its destructor tries to destroy that object again.
    so then I should allocate a new memory first before on my assignment operator and not just assigning it?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by -EquinoX- View Post
    so then I should allocate a new memory first before on my assignment operator and not just assigning it?
    The point is, if you have mutiple object responsible for deleting a single object then multiple delete's will invariably occur (you could instead use a pointer-to-a-pointer, which would allow you to delete, then nullify the shared pointer, but just keep in mind that you would need to also 'new' the pointed-to pointer as well). The best solution is a smart pointer, though (you might look into the boost for a good shared pointer implementation).

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    ...you also don't need to test for NULL prior to deletion.
    But you should set it to 0 ot NULL after deletion.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Bubba View Post
    But you should set it to 0 ot NULL after deletion.
    That is a good habbit, but then again it's also utterly pointless in a destructor. Still, it can't hurt...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21