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

时间:2014-01-19 06:00:16

标签: c++ vector stl fault

我对这个问题感到有点难过......我的程序在特定环境下崩溃了。根据堆栈跟踪判断,在破坏静态分配的std::vector<double>类型期间,问题似乎存在。堆栈中的最低帧看起来保持指向向量中第一个值的指针。我已经附加了帧堆栈和相关代码来帮助调试。这是我到目前为止所发现的:

  1. 在静态分配std::vector<double>

  2. 的重新分配时,似乎发生了错误访问
  3. 我在这里通过引用传递值,它几乎看起来像是通过之前的调用隐式释放了向量,但现在只是一个问题(不确定)

  4. 这是框架&amp;代码:

    #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帧&amp; 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);     
    

1 个答案:

答案 0 :(得分:0)

谢谢Turix,

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

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

启用此功能会在统计函数中捕获到内存错误,我写的是在std :: vector的末尾不恰当地写入数据。

谢谢!