title: C++ Program crashes when accessing object after copy
body: <p>I'm having a problem which I think is related to creating a copy of an object. I have a map class that contains a std::map with a bunch of classes defining the different tile types indexed by their ID, and a bunch of tiles in a vector each with a pointer to one of those classes. Additionally I have another class, Scenario, that contains the map. </p>
  
  <p>The problem comes when I create a Scenario object and try to do anything with its map. There must be some problem when copying the pointers but I haven't been able to figure it out.</p>
  
  <p>I have tried to reduce the code to what's necessary to reproduce the problem:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class TileInfo {
      string name;
  
  public:
      int id;
      TileInfo() {};
      TileInfo(int id, string name);
  
      string getName() const;
  };
  
  TileInfo::TileInfo(int id, string name)
  {
      this-&gt;id = id;
      this-&gt;name = name;
  }
  
  string TileInfo::getName() const 
  {
      return name;
  }
  
  class Tile
  {
  public:
      TileInfo* info;
  
      Tile() {};
      Tile(const Tile &amp; other, map&lt;int,TileInfo&gt; &amp; info);
      Tile (TileInfo* info);
  
      string getName() const;
  };
  
  Tile::Tile (TileInfo* tileInfo)
  {
      this-&gt;info = tileInfo;
  }
  
  Tile::Tile( const Tile &amp; other, map&lt;int,TileInfo&gt; &amp; tileInfo )
  {
      map&lt;int,TileInfo&gt;::iterator it = tileInfo.find(other.info-&gt;id);
  
      if(it == tileInfo.end())
          this-&gt;info = NULL;
      else
          this-&gt;info = &amp;(it-&gt;second);
  }
  
  string Tile::getName() const 
  {
      return info-&gt;getName();
  }
  
  class Map
  {
      vector &lt;vector &lt;Tile&gt;&gt; tiles;
      map &lt;int, TileInfo&gt; tileInfo;
  
  public:
      void print() const;
  
      Map() {};
      Map(map&lt;int,TileInfo&gt; tileInfo);
      Map(const Map &amp; other);
  
      string getName() const;
      void loadTiles();
  };
  
  Map::Map(map&lt;int,TileInfo&gt; tileInfo)
  {
      this-&gt;tileInfo = tileInfo;
  }
  
  Map::Map(const Map &amp; other)
  {
      this-&gt;tileInfo = other.tileInfo;
  
      for(unsigned int i = 0; i &lt; other.tiles.size(); i++)
      {
          vector&lt;Tile&gt; line;
  
          for(unsigned int j = 0; j &lt; other.tiles[i].size(); j++)
          {
              Tile tmpTile = Tile(other.tiles[i][j],tileInfo);
              line.push_back(tmpTile);
          }
  
          this-&gt;tiles.push_back(line);
      }
  }
  
  void Map::print() const
  {
      for(unsigned int i = 0; i &lt; tiles.size(); i++)
      {
          for(unsigned int j = 0; j &lt; tiles[i].size(); j++)
          {
              cout &lt;&lt; "Tile at " &lt;&lt; i &lt;&lt; ", " &lt;&lt; j &lt;&lt; " is a "; cout &lt;&lt; tiles[i][j].getName() &lt;&lt; endl;
          }
      }
  }
  
  void Map::loadTiles()
  {
      for(unsigned int i = 0; i &lt; 3; i++)
      {
          vector &lt;Tile&gt; line;
          for(unsigned int j = 0; j &lt; 3; j++)
          {
              map&lt;int,TileInfo&gt;::iterator ite = tileInfo.find(i);
              if( ite!=tileInfo.end())
                  line.push_back(Tile(&amp;(ite-&gt;second)));
              else
              {
                  line.push_back(Tile(NULL));
              }
          }
  
          tiles.push_back(line);
      }
  }
  
  class Scenario
  {
  public:
      Map map;
      Scenario() {};
      Scenario(Map map);
  };
  
  Scenario::Scenario(Map map)
  {
      this-&gt;map = map;
      this-&gt;map.print();
  }
  
  Map createMap()
  {
      map&lt;int,TileInfo&gt; tmpInfo;
  
      for(unsigned int i = 0; i &lt; 3; i++)
      {
          tmpInfo.insert(pair&lt;int,TileInfo&gt;(i,TileInfo(i,"testname")));
      }
  
      Map rtrnVal = Map(tmpInfo);
  
      rtrnVal.loadTiles();
  
      return rtrnVal;
  }
  
  int main()
  {
      Map newMap = createMap();
  
      Scenario newScenario = Scenario(newMap);
      newScenario.map.print();
      int input;
      cin &gt;&gt; input;
  }
  </code></pre>
  
  <p>The print() at the end of the Scenario constructor prints the map's contents correctly, but the one after the scenario is assigned causes a crash. Any help would be appreciated.</p>