EXC_BAD_ACCESS 在 std::vector <double>的静态声明意外破坏时

EXC_BAD_ACCESS upon unexpected destruction of static declaration of std::vector<double>

本文关键字:静态 声明 意外 gt lt ACCESS BAD std EXC vector double      更新时间:2023-10-16

我对此有点困惑。。。我的程序在特定情况下崩溃。根据堆栈跟踪判断,问题似乎是在静态分配的std::vector<double>类型的销毁过程中出现的。堆栈中最低的帧看起来包含指向向量中第一个值的指针。我已经附加了框架堆栈和相关代码来帮助调试。以下是我迄今为止的发现:

  1. 似乎在静态分配的std::vector<double>的解除分配时发生了错误访问

  2. 我在这里通过引用传递值,看起来向量似乎是由以前的调用隐式释放的,但现在只是一个问题(不确定)

这是框架&代码:

#0  0x00007fff8b83a19a in tiny_free_list_remove_ptr ()
#1  0x00007fff8b835fa1 in szone_free_definite_size ()
#2  0x0000000100003e70 in __gnu_cxx::new_allocator<double>::deallocate(double*, unsigned long) at /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:97
#3  0x0000000100003dce in std::_Vector_base<double, std::allocator<double> >::_M_deallocate(double*, unsigned long) at /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/stl_vector.h:137
#4  0x0000000100003d5c in std::_Vector_base<double, std::allocator<double> >::~_Vector_base() at /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/stl_vector.h:123
#5  0x0000000100005d9c in std::vector<double, std::allocator<double> >::~vector() at /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/stl_vector.h:272
#6  0x0000000100003655 in std::vector<double, std::allocator<double> >::~vector() at /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/stl_vector.h:271
#7  0x0000000100008c87 in tExplore::printStatsHeader(std::vector<transaction*, std::allocator<transaction*> >&) at /Users/blah/Dropbox/Code/Transactulator/tExplore.cpp:284
#8  0x00000001000080d7 in tExplore::stats(std::vector<transaction*, std::allocator<transaction*> >&) at /Users/blah/Dropbox/Code/Transactulator/tExplore.cpp:298
#9  0x0000000100006ab5 in tExplore::menu(std::vector<std::pair<std::string, std::vector<transaction*, std::allocator<transaction*> > (*)()>, std::allocator<std::pair<std::string, std::vector<transaction*, std::allocator<transaction*> > (*)()> > >, void (*)(std::vector<transaction*, std::allocator<transaction*> >&)) at /Users/blah/Dropbox/Code/Transactulator/tExplore.cpp:119
#10 0x0000000100006de0 in tExplore::start(tdata*) at /Users/blah/Dropbox/Code/Transactulator/tExplore.cpp:147

第2帧:

// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type)
{ ::operator delete(__p); }           <—- EXC_BAD_ACCESS= 0x1000000008

帧2值:

this    __gnu_cxx::new_allocator<double> *  0x7fff5fbff598  0x00007fff5fbff598
__p     __gnu_cxx::new_allocator<double>::pointer   0x1005004d0 0x00000001005004d0
*__p    double   -20.96999931335449   -20.96999931335449

第3帧:

void
_M_deallocate(_Tp* __p, size_t __n)
{
if (__p)
_M_impl.deallocate(__p, __n);         <—- EXC_BAD_ACCESS= 0x1000000008

第4帧:

~_Vector_base()
{ _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
- this->_M_impl._M_start); }      <—- EXC_BAD_ACCESS= 0x1000000008

第5帧&6:

~vector()
{ std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
_M_get_Tp_allocator()); }         <—- EXC_BAD_ACCESS= 0x1000000008

第7帧:

void tExplore::printStatsHeader(tvec& data)
{
size_t total = data.size();
double amt_sum;
...
double amt_tmp;
std::vector<double> amt_vec;
//for (tvec::iterator it = data.begin(); it != data.end(); ++it) {
//    amt_tmp = (*it)->getAmount();
//    amt_vec.push_back(amt_tmp);
//}
for (int i = 0; i < total; i++) {
amt_tmp = data.at(i)->getAmount();
amt_vec.push_back(amt_tmp);
}
amt_sum = sstats::sum(amt_vec);
...
amt_min = sstats::min(amt_vec);
clear();
std::cout << "Summary Statistics..." << endl;
...
std::cout << "tMin:      $" << std::fixed << std::setprecision(2) << amt_min    << endl;
}                               <—- EXC_BAD_ACCESS= 0x1000000008

第8帧:

void tExplore::stats(tvec& v)
{
menuObj mObj;
int tmp; /* tmp */
mObj.push_back(make_pair(std::string(MENU_HEADER), nullptr));
....
mObj.push_back(make_pair("6. Remove via date query", nullptr));
printStatsHeader(v);       <—- EXC_BAD_ACCESS= 0x1000000008
printMenu(mObj, false);     

谢谢Turix,

我能够通过启用卫士malloc来解决我的问题。我不知道这是什么,发现这个文档非常有用:

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/libgmalloc.3.html

启用此功能时,我在编写的统计函数中发现了一个内存错误,该错误不恰当地将数据从std::vector的末尾写入。

谢谢!