使用向量时出现奇怪的运行时错误

Strange runtime error when using vectors

本文关键字:运行时错误 向量      更新时间:2023-10-16

当我与 mpi 并行运行时,我在运行时的代码遇到了一个非常奇怪的问题:

*** glibc detected *** ./QuadTreeConstruction: munmap_chunk(): invalid pointer: 0x0000000001fbf180 ***
======= Backtrace: =========
/lib/libc.so.6(+0x776d6)[0x7f38763156d6]
./QuadTreeConstruction(_ZN9__gnu_cxx13new_allocatorImE10deallocateEPmm+0x20)[0x423f04]
./QuadTreeConstruction(_ZNSt13_Bvector_baseISaIbEE13_M_deallocateEv+0x50)[0x423e72]
./QuadTreeConstruction(_ZNSt13_Bvector_baseISaIbEED2Ev+0x1b)[0x423c79]
./QuadTreeConstruction(_ZNSt6vectorIbSaIbEED1Ev+0x18)[0x4237d2]
./QuadTreeConstruction(_Z22findLocalandGhostCellsRK8QuadTreeRK6ArrayVIiES5_iRS3_S6_+0x849)[0x41dbbd]
./QuadTreeConstruction(main+0xa32)[0x41ca49]
/lib/libc.so.6(__libc_start_main+0xfe)[0x7f38762bcd8e]
./QuadTreeConstruction[0x41b029]

我的代码是 valgrind 清理 mpi 库内部的一些错误(我使用 OpenMpi,我以前见过很多次,但它们从未成为问题;见 http://www.open-mpi.org/faq/?category=debugging#valgrind_clean)。当我在串行中运行时,我没有任何问题。

我已经能够使用 gdb 将问题追踪到SIGABORT系统调用,这是代码中断时的堆栈:

0   raise   raise.c 64  0x7f4bd8655ba5  
1   abort   abort.c 92  0x7f4bd86596b0  
2   __libc_message  libc_fatal.c    189 0x7f4bd868f65b  
3   malloc_printerr malloc.c    6283    0x7f4bd86996d6  
4   __gnu_cxx::new_allocator<unsigned long>::deallocate new_allocator.h 95  0x423f04    
5   std::_Bvector_base<std::allocator<bool> >::_M_deallocate    stl_bvector.h   444 0x423e72    
6   std::_Bvector_base<std::allocator<bool> >::~_Bvector_base   stl_bvector.h   430 0x423c79    
7   std::vector<bool, std::allocator<bool> >::~vector   stl_bvector.h   547 0x4237d2    
8   findLocalandGhostCells  mpi_partition.cpp   249 0x41dbbd    
9   main    mpi_partition.cpp   111 0x41ca49    

这听起来像是内存损坏,但我完全不知道是什么原因造成的。基本上,代码在看起来像这样的函数中中断:

void findLocalandGhostCells(){
    std::vector<bool> foo(fooSize,false);
    // do stuff with foo; nothing crazy -- I promise
    return;
}

有人知道我现在应该做什么吗?:(

如果您非常确定向量操作本身是正确的且不疯狂的,请尝试逐步跟踪向量的成员。您的其他一些操作可能损坏了矢量的内存块。例如,入侵矢量记忆的memcpy。