title: std::map iterate and delete valgrind errors
body: <p>I have a simple program which deletes the items in a std::map while iterating over the Map. If I am not wrong forward iterators are not invalidated by erase in a map. But valgrind throws invalid read errors. Can someone explain why.</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;iostream&gt;
  
  typedef std::map&lt;std::string, int&gt; SourceMap;
  int main(int argc, char *argv[])
  {
      SourceMap sourceMap;
      sourceMap["item1"] = 1;
      sourceMap["item2"] = 2;
      sourceMap["item3"] = 3;
      sourceMap["item4"] = 4;
  
  
      for(SourceMap::const_iterator it=sourceMap.begin(); it != sourceMap.end(); ++it)
      {
          sourceMap.erase(it-&gt;first);
      }
  }
  </code></pre>
  
  <p><strong>Valgrind errors:</strong></p>
  
  <pre><code>==31703== Invalid read of size 8
  ==31703==    at 0x3851069E60: std::_Rb_tree_increment(std::_Rb_tree_node_base*) (in /usr/lib64/libstdc++.so.6.0.13)
  ==31703==    by 0x4013D6: std::_Rb_tree_const_iterator&lt;std::pair&lt;std::string const, int&gt; &gt;::operator++() (stl_tree.h:259)
  ==31703==    by 0x40106B: main (map_iterator.cpp:14)
  ==31703==  Address 0x4c2c0b8 is 24 bytes inside a block of size 48 free'd
  ==31703==    at 0x4A0545F: operator delete(void*) (vg_replace_malloc.c:387)
  ==31703==    by 0x402821: __gnu_cxx::new_allocator&lt;std::_Rb_tree_node&lt;std::pair&lt;std::string const, int&gt; &gt; &gt;::deallocate(std::_Rb_tree_node&lt;std::pair&lt;std::string const, int&gt; &gt;*, unsigned long) (new_allocator.h:95)
  ==31703==    by 0x402103: std::_Rb_tree&lt;std::string, std::pair&lt;std::string const, int&gt;, std::_Select1st&lt;std::pair&lt;std::string const, int&gt; &gt;, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, int&gt; &gt; &gt;::_M_put_node(std::_Rb_tree_node&lt;std::pair&lt;std::string const, int&gt; &gt;*) (stl_tree.h:363)
  ==31703==    by 0x4017D0: std::_Rb_tree&lt;std::string, std::pair&lt;std::string const, int&gt;, std::_Select1st&lt;std::pair&lt;std::string const, int&gt; &gt;, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, int&gt; &gt; &gt;::_M_destroy_node(std::_Rb_tree_node&lt;std::pair&lt;std::string const, int&gt; &gt;*) (stl_tree.h:384)
  ==31703==    by 0x4027AB: std::_Rb_tree&lt;std::string, std::pair&lt;std::string const, int&gt;, std::_Select1st&lt;std::pair&lt;std::string const, int&gt; &gt;, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, int&gt; &gt; &gt;::erase(std::_Rb_tree_iterator&lt;std::pair&lt;std::string const, int&gt; &gt;) (stl_tree.h:1348)
  ==31703==    by 0x401FF2: std::_Rb_tree&lt;std::string, std::pair&lt;std::string const, int&gt;, std::_Select1st&lt;std::pair&lt;std::string const, int&gt; &gt;, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, int&gt; &gt; &gt;::erase(std::_Rb_tree_iterator&lt;std::pair&lt;std::string const, int&gt; &gt;, std::_Rb_tree_iterator&lt;std::pair&lt;std::string const, int&gt; &gt;) (stl_tree.h:1388)
  ==31703==    by 0x4016A7: std::_Rb_tree&lt;std::string, std::pair&lt;std::string const, int&gt;, std::_Select1st&lt;std::pair&lt;std::string const, int&gt; &gt;, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, int&gt; &gt; &gt;::erase(std::string const&amp;) (stl_tree.h:1374)
  ==31703==    by 0x40141C: std::map&lt;std::string, int, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, int&gt; &gt; &gt;::erase(std::string const&amp;) (stl_map.h:582)
  ==31703==    by 0x40105C: main (map_iterator.cpp:16)
  ==31703==
  ==31703==
  ==31703== HEAP SUMMARY:
  ==31703==     in use at exit: 0 bytes in 0 blocks
  ==31703==   total heap usage: 8 allocs, 8 frees, 312 bytes allocated
  ==31703==
  ==31703== All heap blocks were freed -- no leaks are possible
  </code></pre>