title: std::map causing memory leaks?
body: <h2>EDIT</h2>
  
  <p>To make this post a bit more constructive, and let it possibly help others in the future:</p>
  
  <p><strong>The problem was this:</strong></p>
  
  <pre><code>std::map&lt;Point2, Prop*&gt; mm;
  std::pair&lt;Point2, Prop*&gt; p;
  
  if(Keydown(VK_LBUTTON)) {
       p.first = pos; p.second = new Prop();
       mm.insert(p))
  }
  </code></pre>
  
  <p>So even though the map would get iterated over and deallocate all the Prop* pointers in the end, there were times where the insertion would fail (because a pair with the same key might already be in the tree). That means the new Prop() created, would get orphaned.</p>
  
  <p><strong>Solutions:</strong></p>
  
  <p>1) either always use std::shared_ptr (probably best solution)</p>
  
  <p>2) or do this:</p>
  
  <pre><code>std::map&lt;Point2, Prop*&gt; mm;
  std::pair&lt;Point2, Prop*&gt; p;
  
  if(Keydown(VK_LBUTTON)) {
       p.first = pos; p.second = new Prop();
       if(mm.insert(p).second == false) {
            delete p.second;
       }
  }
  </code></pre>
  
  <p><em>Thanks to SO user <strong>parapura rajkumar</em></strong></p>
  
  <hr>
  
  <h2>Original Question:</h2>
  
  <p>I'm having memory leaks in my application and I don't know what's causing them! I thought I am deallocating everything I have to. The weird part is: I don't have memory leaks every time I'm running my application.</p>
  
  <p>In short, this is what my app does: </p>
  
  <p>On initialisation, it creates <code>numRows</code> times <code>numColumns</code> <code>new Tile()</code> inside a <code>TileList</code>.
  When the mouse is hovering over some position on the screen, and the left mouse button is held, it adds a <code>std::pair&lt;Point2, Prop*&gt; p</code> , with <code>p.second = new Prop()</code> to an <code>std::map</code>.</p>
  
  <p>Sometimes I can just add a whole bunch of props and exit the app without any leaks. Sometimes I would add the same props as before, and it will have memory leaks upon exit.</p>
  
  <p>Please help.
  Here is the relevant code:</p>
  
  <p><em>If you need to see a specific part of my code, just comment it, and I'll edit the question</em></p>
  
  <p><strong>PropList.h</strong></p>
  
  <pre><code>class PropList
  {
  protected:
      std::map&lt;Point2, Prop*&gt; m_Props_m;
  
  public:
      PropList(){}
      virtual ~PropList();
  
      bool PropAdd(std::pair&lt;Point2, Prop*&gt; p)
      {
          pair&lt;map&lt;Point2, Prop*&gt;::iterator,bool&gt; ret = m_Props_m.insert(p);
          return ret.second;
      }
      bool PropRemove( const Point2&amp; pos );
      bool HasProp( const Point2&amp; pos );
  
      void Tick();
  
  protected:
  
  };
  
  static void PropRelease(const std::pair&lt;Point2, Prop*&gt;&amp; p) {
      delete p.second;
  }
  </code></pre>
  
  <p><strong>PropList.cpp</strong></p>
  
  <pre><code>PropList::~PropList()
  {
      std::for_each(m_Props_m.begin(), m_Props_m.end(), &amp;PropRelease);
  }
  
  bool PropList::PropRemove( const Point2&amp; pos )
  {
      std::map&lt;Point2, Prop*&gt;::iterator it = m_Props_m.find(pos);
      if (it == m_Props_m.end()) {
          return false;
      }
      delete (*it).second;
      m_Props_m.erase(it);
      return true;
  }
  </code></pre>
  
  <p><strong>TileList.h</strong></p>
  
  <pre><code>class TileList
  {
  protected:
      std::vector&lt;std::vector&lt;Tile*&gt; &gt; m_Tiles_v;
      PropList m_PropList;
  
      UINT m_iRowNum;
      UINT m_iColNum;
  
  public:
      TileList(UINT numColumns, UINT numRows);
      virtual ~TileList();
  
      //Props
      void PropAdd(std::pair&lt;Point2, Prop*&gt; p);
      void PropRemove(const Point2&amp; pos);
      bool HasProp(const Point2&amp; pos);
      void Tick();
  
      UINT GetNumRows(){return m_iRowNum;}
      UINT GetNumCols(){return m_iColNum;}
  
  protected:
  };
  </code></pre>
  
  <p><strong>TileList.cpp</strong></p>
  
  <pre><code>TileList::TileList(UINT numColumns, UINT numRows)
      :m_iRowNum(numRows)
      ,m_iColNum(numColumns)
  {
      for (UINT i = 0; i &lt; numRows; ++i) {
          m_Tiles_v.push_back(std::vector&lt;Tile*&gt;());
          for (UINT j = 0; j &lt; numColumns; ++j) {
              m_Tiles_v[i].push_back(new Tile());
          }
      }
  }
  
  TileList::~TileList()
  {
      BOOST_FOREACH(std::vector&lt;Tile*&gt; col_tiles_v, m_Tiles_v)
      {
          BOOST_FOREACH(Tile* pTile, col_tiles_v)
          {
              delete pTile;
          }
      }
  }
  
  void TileList::PropAdd(std::pair&lt;Point2, Prop*&gt; p)
  {
      if(m_PropList.PropAdd(p)) {
          m_Tiles_v[p.first.y][p.first.x]-&gt;setOccupied(true);
      }
  }
  
  void TileList::PropRemove(const Point2&amp; pos) 
  {
      if(m_PropList.PropRemove(pos)) {
          m_Tiles_v[pos.y][pos.x]-&gt;setOccupied(false);
      }
  }
  </code></pre>