std :: map迭代并删除valgrind错误

时间:2014-04-04 14:23:29

标签: c++ valgrind

我有一个简单的程序,它在迭代Map时删除std :: map中的项目。如果我没有错误,则前导迭代器不会因地图中的擦除而失效。但是valgrind会抛出无效的读错误。有人可以解释原因吗。

#include <map>
#include <iostream>

typedef std::map<std::string, int> 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->first);
    }
}

Valgrind错误:

==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<std::pair<std::string const, int> >::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<std::_Rb_tree_node<std::pair<std::string const, int> > >::deallocate(std::_Rb_tree_node<std::pair<std::string const, int> >*, unsigned long) (new_allocator.h:95)
==31703==    by 0x402103: std::_Rb_tree<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::_M_put_node(std::_Rb_tree_node<std::pair<std::string const, int> >*) (stl_tree.h:363)
==31703==    by 0x4017D0: std::_Rb_tree<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::_M_destroy_node(std::_Rb_tree_node<std::pair<std::string const, int> >*) (stl_tree.h:384)
==31703==    by 0x4027AB: std::_Rb_tree<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::erase(std::_Rb_tree_iterator<std::pair<std::string const, int> >) (stl_tree.h:1348)
==31703==    by 0x401FF2: std::_Rb_tree<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::erase(std::_Rb_tree_iterator<std::pair<std::string const, int> >, std::_Rb_tree_iterator<std::pair<std::string const, int> >) (stl_tree.h:1388)
==31703==    by 0x4016A7: std::_Rb_tree<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::erase(std::string const&) (stl_tree.h:1374)
==31703==    by 0x40141C: std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::erase(std::string const&) (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

2 个答案:

答案 0 :(得分:7)

这已被讨论过死亡。您不能使用无效的迭代器。写得像这样:

for(SourceMap::const_iterator it = sourceMap.begin();
    it != sourceMap.end();  /* no hoist */
   /* no increment */ )
{
    sourceMap.erase(it++);
}

可替换地:

// as before
{
    it = sourceMap.erase(it);
}

或者(因为你没有任何擦除条件):

sourceMap.clear();

答案 1 :(得分:0)

你做错了什么:

在迭代地图时,擦除当前迭代器指向的键。这样做会使该迭代器失效。

map :: erase(iterator-&gt; first)与map :: erase(iterator)具有相同的效果,但后者更合适,效率更高。

注意:请参阅Kerrek SB的答案