for循环中删除map中的元素valgrind检测提示error:Invalid read of size 8

 更新时间:2020年4月25日 17:34  点击:1261

废话不多说,先看下面一段代码

#include <iostream>
#include <map>
using namespace std;
class A
{
public:
typedef std::map<int, string> myMap;
void mapInsert(int i, string s)
{
map.insert(std::make_pair(i, s));
}
void deleteMap()
{
for (myMap::iterator it = map.begin(); it != map.end(); ++it)
{
map.erase(it->first);
}
}
private:
myMap map;
};
int main()
{
A a;
a.mapInsert(1, "1");
a.mapInsert(2, "2");
a.mapInsert(3, "3");
a.mapInsert(4, "4");
a.mapInsert(5, "5");
a.deleteMap();
return 0;
}

上述代码编译运行皆没有问题,但是用valgrind检测会提示错误:

valgrind --tool=memcheck --leak-check=full --track-origins=yes ./test # ~/test
==723953== Memcheck, a memory error detector
==723953== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==723953== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==723953== Command: ./test
==723953== 
==723953== Invalid read of size 8
==723953== at 0x3431C69E60: std::_Rb_tree_increment(std::_Rb_tree_node_base*) (tree.cc:60)
==723953== by 0x40131C: std::_Rb_tree_iterator<std::pair<int const, std::string> >::operator++() (in /home/thm/test/test)
==723953== by 0x40117C: A::deleteMap() (in /home/thm/test/test)
==723953== by 0x400F4B: main (in /home/thm/test/test)
==723953== Address 0x4c580b8 is 24 bytes inside a block of size 48 free'd
==723953== at 0x4A06016: operator delete(void*) (vg_replace_malloc.c:480)
==723953== by 0x401E23: __gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, std::string> > >::deallocate(std::_Rb_tree_node<std::pair<int const, std::string> >*, unsigned long) (in /home/thm/test/test)
==723953== by 0x401C99: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_put_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test)
==723953== by 0x401AA6: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_destroy_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test)
==723953== by 0x401729: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test)
==723953== by 0x40134C: std::map<int, std::string, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test)
==723953== by 0x401170: A::deleteMap() (in /home/thm/test/test)
==723953== by 0x400F4B: main (in /home/thm/test/test)
==723953== 
==723953== Invalid read of size 8
==723953== at 0x3431C69E80: std::_Rb_tree_increment(std::_Rb_tree_node_base*) (tree.cc:68)
==723953== by 0x40131C: std::_Rb_tree_iterator<std::pair<int const, std::string> >::operator++() (in /home/thm/test/test)
==723953== by 0x40117C: A::deleteMap() (in /home/thm/test/test)
==723953== by 0x400F4B: main (in /home/thm/test/test)
==723953== Address 0x4c580a8 is 8 bytes inside a block of size 48 free'd
==723953== at 0x4A06016: operator delete(void*) (vg_replace_malloc.c:480)
==723953== by 0x401E23: __gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, std::string> > >::deallocate(std::_Rb_tree_node<std::pair<int const, std::string> >*, unsigned long) (in /home/thm/test/test)
==723953== by 0x401C99: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_put_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test)
==723953== by 0x401AA6: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_destroy_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test)
==723953== by 0x401729: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test)
==723953== by 0x40134C: std::map<int, std::string, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test)
==723953== by 0x401170: A::deleteMap() (in /home/thm/test/test)
==723953== by 0x400F4B: main (in /home/thm/test/test)
==723953== 
==723953== 
==723953== HEAP SUMMARY:
==723953== in use at exit: 0 bytes in 0 blocks
==723953== total heap usage: 10 allocs, 10 frees, 370 bytes allocated
==723953== 
==723953== All heap blocks were freed -- no leaks are possible
==723953== 
==723953== For counts of detected and suppressed errors, rerun with: -v
==723953== ERROR SUMMARY: 8 errors from 2 contexts (suppressed: 6 from 6)

Why?

此代码可以实现功能要求,但是健壮性并不好,假设在map.erase之后再次使用map当前的iterator,即

void deleteMap()
{
for (myMap::iterator it = map.begin(); it != map.end(); ++it)
{
map.erase(it->first);
std::cout << "map.first=" << it->first << " map.second=" << it->second << std::endl;
}
}

代码运行就会出现错误,因为it目前指向的对象已经被删掉了。

为了避免程序出现这样的错误,我们应该保证在iterator指向的对象被删掉之前,iterator已经向前移位一。

程序改成如下即可:

void deleteMap()
{
for (myMap::iterator it = map.begin(); it != map.end();)
{
map.erase(it++->first);
}
} 


void deleteMap()
{
for (myMap::iterator it = map.begin(); it != map.end();)
{
int i = it->first;
++it;
map.erase(i);
}
}

以上所述是小编给大家介绍的for循环中删除map中的元素valgrind检测提示error:Invalid read of size 8 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对猪先飞网站的支持!

[!--infotagslink--]

相关文章

  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • 解决使用OpenCV中的imread()内存报错问题

    这篇文章主要介绍了解决使用OpenCV中的imread()内存报错问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C#多线程之Thread类详解

    这篇文章主要为大家详细介绍了C#多线程之Thread类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • C# 数组中的 indexOf 方法及使用

    这篇文章主要介绍了C# 数组中的 indexOf 方法以及indexof方法的使用讲解,需要的朋友可以参考下...2020-06-25
  • JS如何监听div的resize事件详解

    这篇文章主要给大家介绍了关于JS如何监听div的resize事件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-04
  • 详解JavaScript中数组和字符串的lastIndexOf()方法使用

    这篇文章主要介绍了详解JavaScript中数组和字符串的lastIndexOf()方法使用,文中特别提及了IE8的兼容问题以及for in的使用问题,需要的朋友可以参考下...2016-03-15
  • C#基础知识之GetType与typeof的区别小结

    在比较对象时,需要了解他们的类型,才能决定他们的值是否能比较。所有的类都从System.Object中继承了GetType()方法,常常与typeo()运算符一起使用。这篇文章主要给大家介绍了关于C#基础知识之GetType与typeof区别的相关资料,需要的朋友可以参考下...2021-06-01
  • 详解C#中 Thread,Task,Async/Await,IAsyncResult的那些事儿

    本文主要介绍了C#中 Thread,Task,Async/Await,IAsyncResult的相关知识。具有一定的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • IOS开发之多线程NSThiread GCD NSOperation Runloop

    这篇文章主要介绍了IOS多线程开发,主要用到NSThiread、GCD、 NSOperation、Runloop,有详细的原理解析和实例代码,对多线程感兴趣的同学,可以参考下...2021-04-14
  • JavaScript中isPrototypeOf函数

    这篇文章主要介绍了JavaScript中isPrototypeOf函数,isPrototypeOf() 是 Object函数(类)的下的一个方法,用于判断当前对象是否为另外一个对象的原型,如果是就返回 true,否则就返回 false,下面来看看详细内容,需要的朋友可以参考一下...2021-11-05
  • 优化mysql之key_buffer_size设置

    这篇文章主要介绍了优化mysql之key_buffer_size设置的相关资料,需要的朋友可以参考下...2016-06-12
  • c# Thread类的用法详解

    这篇文章主要介绍了c# Thread类的用法的相关资料,帮助大家更好的理解和学习c#的相关知识,感兴趣的朋友可以了解下...2020-12-08
  • JavaScript indexOf()原理及使用方法详解

    这篇文章主要介绍了JavaScript indexOf()原理及使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-09
  • 快速解决cv2.imread()读取图像为BGR的问题

    这篇文章主要介绍了快速解决cv2.imread()读取图像为BGR的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • Java Thread之Sleep()使用方法总结

    这篇文章主要介绍了Java Thread之Sleep()使用方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-05-15
  • C#中sqlDataRead 的三种方式遍历读取各个字段数值的方法

    这篇文章主要介绍了C#中 sqlDataRead 的三种方式遍历读取各个字段数值的方法,每种方法给大家介绍的都非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • window环境redis通过AOF恢复数据的方法

    这篇文章主要介绍了window环境redis通过AOF恢复数据的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-15
  • 谷歌开源Wycheproof,可自动检测加密库漏洞

    12 月 19 日,谷歌发布了 Wycheproof 项目,它包含一系列安全测试,用来检测加密库(cryptographic libraries)软件是否存在已知的攻击漏洞。该项目以世界上最小的山 Wycheproof (位于澳大利亚)命名,并已开源。...2016-12-21
  • Linux内核宏Container_Of的详细解释

    我们在分析Linux内核链表的时候注意到内核在求解结构体偏移的时候巧妙的使用了container_of宏定义,本文我们就来详细剖析下内核到底是如何求解结构体成员变量的地址的。...2021-09-10
  • 关于Pytorch MaxUnpool2d中size操作方式

    今天小编就为大家分享一篇关于Pytorch MaxUnpool2d中size操作方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-30