patchwork
patchwork

Reputation: 1221

Why is the destructor being called during copy assignmment?

I have defined the following two C++ structs, Room and House. House contains a std::list<Room>.

I create a House object with new. I create a new Room with new. I create another House object with new and attempt to assign the previous House to this one. But for some reason the House destructor gets called... why?

(and then I also get a seg fault).

struct Room
{
   Room()
   : pString(0)
   {
     std::cout << "Room ctor [" << std::hex << this << "]\n";
   }
   Room(const Room& other)
   {
     std::cout << "Room COPY ctor [" << std::hex << this << "] other: " << std::hex << &other << "]\n";
     if(other.pString)
     {
        if(pString)
            delete pString;
        pString = strdup(other.pString);
     }
   }
   Room operator=(const Room& other)
   {
     std::cout << "Room ASSIGNMENT operator [" << std::hex << this << "] other: " << std::hex << &other << "]\n";
     if(this != &other)
     {
         if(other.pString)
         {
            if(pString)
                delete pString;
            pString = strdup(other.pString);
         }
      }
   }

  ~Room()
   { 
     std::cout << "Room dtor [" << std::hex << this << "]\n";
     if(pString)
        delete pString;
   }
   char * pString;
};

/// House struct ////////////////////////////
struct House
{
   House()
   {
     std::cout << "House ctor [" << std::hex << this << "]\n";
   }
   House(const House& other)
   {
     std::cout << "House COPY ctor [" << std::hex << this << "] other: " << std::hex << &other << "]\n";
     roomlist = other.roomlist;
   }
   House operator=(const House& other)
   {
     std::cout << "House ASSIGNMENT ctor [" << std::hex << this << "] other: " << std::hex << &other << "]\n";
     if(this != &other)
     {
       roomlist = other.roomlist;
     }
   }

  ~House()
   { 
     std::cout << "House dtor [" << std::hex << this << "]\n";
   }
   std::list<Room> roomlist;
};

The code to test this is below:

       House * pCurHouse = new House;          
       Room * pIkeaRm = new Room;
       pIkeaRm->pString = strdup("IKEA ROOM");
       std::cout << "Room created\n\n\n";
       pCurHouse->roomlist.push_back(*pIkeaRm);
       House * pOtherHouse = new House;       


       std::cout << "assigning current house to this house... \n";
       *pOtherHouse = *pCurHouse;
       std::cout << "House assigned. \n\n\n";

I never see the debug "House assigned". I see instead:

assigning current house to this house... 
House ASSIGNMENT operator [0x20753a0] other: 0x2075210]
Room COPY constructor [0x2075400] other: 0x2075310]
House destructor [0x7fff36a7a6a0]   //// which House object is that????
Room destructor [0x402580]
Segmentation fault (core dumped)

Upvotes: 0

Views: 91

Answers (1)

BartoszKP
BartoszKP

Reputation: 35891

This is the destructor of the return value of your House House::operator= method. You forgot however the return statements in both of your assignment operators (usually return *this;), and hence the crash. As noted in the comments by Matt McNabb of course usually you also return a reference, not a copy.

Upvotes: 1

Related Questions