简体   繁体   中英

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

I'm a bit stumped on this one... My program crashes under a particular set of circumstances. Judging by the stack traces, it appears that the problem arrises during the destruction of a statically allocated std::vector<double> type. The lowest frame in the stack looks to hold a pointer to the first value in the vector. I've attached the frame stack and relevant code to help with the debugging. Here is what I've found so far:

  1. It appears that bad access occurs on the deallocation of a statically allocated std::vector<double>

  2. I am passing values around by reference here and it almost seems like the vector is being deallocated implicitly by an earlier call but is just a problem now (unsure)

Here is the frame & code:

#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

frame 2:

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

frame 2 values:

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

frame 3:

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

frame 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

frame 5 & 6:

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

frame 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

frame 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);     

Thank you Turix,

I was able to fix my problem by enabling guard malloc. I did not know what this is and found this documentation pretty helpful:

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

Enabling this caught a memory error in a statistics function I had written that inappropriately wrote data off the end of a std::vector.

Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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