繁体   English   中英

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

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

我对此感到有些困惑...在特定情况下,我的程序崩溃了。 从堆栈跟踪来看,问题似乎是在销毁静态分配的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的末尾不适当地写入了数据。

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM