关于一个内存泄露的问题
发布日期:2022-02-07 06:39:47 浏览次数:2 分类:技术文章

本文共 5067 字,大约阅读时间需要 16 分钟。

是这样子的

这样子的代码居然能够正常运行

#include 
#include
using namespace std;vector
::iterator fun(vector
t){ return t.begin()+2;//返回一个局部对象的迭代器}int main(){ vector
test = {
1,2,3,4}; auto p1 = fun(test); cout << *p1 << endl;//对之前局部的迭代器进行解引用,应该是不行的}// 但是能够正常的输出3

不清楚为什么

使用内存检测工具检查了一下

valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./test.out==4677== Memcheck, a memory error detector==4677== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.==4677== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info==4677== Command: ./test.out==4677== ==4677== Invalid read of size 4==4677==    at 0x400BA8: main (t.cpp:13)==4677==  Address 0x5ab4cd8 is 8 bytes inside a block of size 16 free'd==4677==    at 0x4C2F160: operator delete(void*) (vg_replace_malloc.c:576)==4677==    by 0x4016AB: __gnu_cxx::new_allocator
::deallocate(int*, unsigned long) (new_allocator.h:110)==4677== by 0x4015B8: std::allocator_traits
>::deallocate(std::allocator
&, int*, unsigned long) (alloc_traits.h:517)==4677== by 0x4013A5: std::_Vector_base
>::_M_deallocate(int*, unsigned long) (stl_vector.h:178)==4677== by 0x401094: std::_Vector_base
>::~_Vector_base() (stl_vector.h:160)==4677== by 0x400EB2: std::vector
>::~vector() (stl_vector.h:425)==4677== by 0x400B9B: main (t.cpp:12)==4677== Block was alloc'd at==4677== at 0x4C2E1D6: operator new(unsigned long) (vg_replace_malloc.c:334)==4677== by 0x4016EF: __gnu_cxx::new_allocator
::allocate(unsigned long, void const*) (new_allocator.h:104)==4677== by 0x40160D: std::allocator_traits
>::allocate(std::allocator
&, unsigned long) (alloc_traits.h:491)==4677== by 0x401435: std::_Vector_base
>::_M_allocate(unsigned long) (stl_vector.h:170)==4677== by 0x401508: std::_Vector_base
>::_M_create_storage(unsigned long) (stl_vector.h:185)==4677== by 0x40122C: std::_Vector_base
>::_Vector_base(unsigned long, std::allocator
const&) (stl_vector.h:136)==4677== by 0x400F19: std::vector
>::vector(std::vector
> const&) (stl_vector.h:320)==4677== by 0x400B7F: main (t.cpp:12)==4677== 3==4677== ==4677== HEAP SUMMARY:==4677== in use at exit: 72,704 bytes in 1 blocks==4677== total heap usage: 4 allocs, 3 frees, 73,760 bytes allocated==4677== ==4677== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1==4677== at 0x4C2DBB6: malloc (vg_replace_malloc.c:299)==4677== by 0x4EC2EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)==4677== by 0x40104E9: call_init.part.0 (dl-init.c:72)==4677== by 0x40105FA: call_init (dl-init.c:30)==4677== by 0x40105FA: _dl_init (dl-init.c:120)==4677== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)==4677== ==4677== LEAK SUMMARY:==4677== definitely lost: 0 bytes in 0 blocks==4677== indirectly lost: 0 bytes in 0 blocks==4677== possibly lost: 0 bytes in 0 blocks==4677== still reachable: 72,704 bytes in 1 blocks==4677== suppressed: 0 bytes in 0 blocks==4677== ==4677== For counts of detected and suppressed errors, rerun with: -v==4677== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

后来拿了另外一个代码进行测试

#include 
#include
using namespace std;vector
::iterator fun(vector
& t){
//修改了,变成传引用 return t.begin()+2;}int main(){ vector
test = {
1,2,3,4}; auto p1 = fun(test); cout << *p1 << endl;}

这个是肯定没问题的

同样的内存检测工具进行

valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./test.out==4743== Memcheck, a memory error detector==4743== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.==4743== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info==4743== Command: ./test.out==4743== 3==4743== ==4743== HEAP SUMMARY:==4743==     in use at exit: 72,704 bytes in 1 blocks==4743==   total heap usage: 3 allocs, 2 frees, 73,744 bytes allocated==4743== ==4743== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1==4743==    at 0x4C2DBB6: malloc (vg_replace_malloc.c:299)==4743==    by 0x4EC2EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)==4743==    by 0x40104E9: call_init.part.0 (dl-init.c:72)==4743==    by 0x40105FA: call_init (dl-init.c:30)==4743==    by 0x40105FA: _dl_init (dl-init.c:120)==4743==    by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)==4743== ==4743== LEAK SUMMARY:==4743==    definitely lost: 0 bytes in 0 blocks==4743==    indirectly lost: 0 bytes in 0 blocks==4743==      possibly lost: 0 bytes in 0 blocks==4743==    still reachable: 72,704 bytes in 1 blocks==4743==         suppressed: 0 bytes in 0 blocks==4743== ==4743== For counts of detected and suppressed errors, rerun with: -v==4743== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

同样能够正常运行

不过内存检测工具还是检查出问题来了

问题:

不知道为什么之前那个代码能够正常运作

转载地址:https://blog.csdn.net/wyfwyf12321/article/details/73359888 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:chrome快捷键总结
下一篇:linux系统备份以及自定义liveCD的制作

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月10日 10时44分46秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章