通过分配大量内存来跟踪堆损坏?

问题描述 投票:0回答:1

在我的程序中,我遇到以下错误:

free(): invalid size
Aborted (core dumped)

正在运行的GDB,我发现这发生在向量的析构函数中:

#0  0x00007ffff58e8c01 in free () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x0000555555dd44e2 in __gnu_cxx::new_allocator<int>::deallocate (this=0x7fffffff6bf0, __p=0x555557117810) at /usr/include/c++/7/ext/new_allocator.h:125
#2  0x0000555555dcfbd7 in std::allocator_traits<std::allocator<int> >::deallocate (__a=..., __p=0x555557117810, __n=1) at /usr/include/c++/7/bits/alloc_traits.h:462
#3  0x0000555555dc85e6 in std::_Vector_base<int, std::allocator<int> >::_M_deallocate (this=0x7fffffff6bf0, __p=0x555557117810, __n=1)
    at /usr/include/c++/7/bits/stl_vector.h:180
#4  0x0000555555dc49e1 in std::_Vector_base<int, std::allocator<int> >::~_Vector_base (this=0x7fffffff6bf0, __in_chrg=<optimized out>)
    at /usr/include/c++/7/bits/stl_vector.h:162
#5  0x0000555555dbc5c9 in std::vector<int, std::allocator<int> >::~vector (this=0x7fffffff6bf0, __in_chrg=<optimized out>) at /usr/include/c++/7/bits/stl_vector.h:435
#6  0x0000555556338081 in Gambit::Printers::HDF5Printer2::get_buffer_idcodes[abi:cxx11](std::vector<Gambit::Printers::HDF5MasterBuffer*, std::allocator<Gambit::Printers::HDF5MasterBuffer*> > const&) (this=0x555556fd8820, masterbuffers=...) at /home/farmer/repos/gambit/copy3/Printers/src/printers/hdf5printer_v2/hdf5printer_v2.cpp:2183

最后一行代码简单地是:

std::vector<int> alllens(myComm.Get_size());

所以首先,我不太明白为什么在这里调用析构函数,但是假设它是向量的动态构建方式的正常部分,那么我猜这个错误一定是由于某种堆损坏造成的。

不过,我还没有完全理解,代码的其他部分之前是否曾非法访问本应分配给该向量的内存?

[第二,我尝试通过Intel Inspector运行此命令,但确实收到一堆标记为“无效的内存访问”和“未初始化的内存访问”的问题,但它们看起来都像我正在使用的库(如HDF5)中的误报。

是否有某种代码内的方法来缩小问题的根源?例如。由于它是由动态内存分配触发的,所以我可以只是在代码中越来越早地开始分配巨大的数组,以尝试在接近崩溃源的地方触发崩溃吗?我尝试搜索类似的东西是否有用或有用,但是没有找到任何东西,所以也许这不是一个好主意?

c++ debugging heap-corruption
1个回答
0
投票

因此,事实证明我正在通过一些MPI例程破坏堆,即,缓冲区长度的参数不正确等等。不幸的是,MPI库中发生了很多疯狂的事情,因此堆检查器(如Intel Inspector)在查找它时没有那么有用。

© www.soinside.com 2019 - 2024. All rights reserved.