开发者

Question about EXC_BAD_ACCESS error in std::vector::push_back on a pointer

std::vector sure is great, hey?

I'm getting an EXC_BAD_ACCESS in using push_back to add an element, though. (I had a similar problem once, looked it up on SO, solved! Sadly, this appears to be a different issue.)

 class BackgroundGroupsHandler {
 public:
     void addBeat(Beat *b);
     vector<beat_display_group*> groups;
 };

(Beat is a simple struct-like class that carries some data around.)

 class beat_display_group {
 public:
     void draw_me(float, float, float, int);
     beat_display_group(int rhythmInt, int beatNumber);
     ~beat_display_group();
     int posy;
 private:
     int rhythmInt;
     int beatNumber;
     int posx;
 };

(beat_display_group crunches some numbers to put each group in the right place on 开发者_开发问答the screen.)

 class BackgroundGroupsHandler {
 public:
     void addBeat(Beat *b);
     vector<beat_display_group*> groups;
 };

And here's the problem:

 void BackgroundGroupsHandler::addBeat(Beat *b) {
      beat_display_group *c = new beat_display_group(b->rhythmInt);

      // EXC_BAD_ACCCESS ON THIS LINE:
      groups.push_back(c);
 }

sometimes gdb takes me to stl_vector.h:

  // [23.2.4.2] capacity
  /**  Returns the number of elements in the %vector.  */
  size_type
  size() const
  { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }

and other times new_allocator.h:

  // _GLIBCXX_RESOLVE_LIB_DEFECTS
  // 402. wrong new expression in [some_] allocator::construct
  void 
  construct(pointer __p, const _Tp& __val) 
  { ::new(__p) _Tp(__val); }

  void 
  destroy(pointer __p) { __p->~_Tp(); }


The problem is most likely that the BackgroundGroupsHandler that you are calling addBeat on is NULL or otherwise an invalid pointer. The problem shows up in the std::vector code because you are using groups, which will be invalid due to the BackgroundGroupsHandler being invalid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜