关于指针上的 std::vector::push_back 中的 EXC_BAD_ACCESS 错误的问题
std::vector 确实很棒,嘿?
不过,我在使用 push_back
添加元素时遇到了 EXC_BAD_ACCESS
。 (我曾经遇到过类似的问题,在 SO 上查找,解决了!遗憾的是,这似乎是一个不同的问题。)
class BackgroundGroupsHandler {
public:
void addBeat(Beat *b);
vector<beat_display_group*> groups;
};
(Beat
是一个简单的类似结构的类,它携带一些数据。)
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
会处理一些数字,将每个组放在屏幕上的正确位置。)
class BackgroundGroupsHandler {
public:
void addBeat(Beat *b);
vector<beat_display_group*> groups;
};
问题是:
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);
}
有时 gdb
会将我带到 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); }
和其他时间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(); }
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(); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题很可能是您调用
addBeat
的BackgroundGroupsHandler
为NULL
或其他无效指针。该问题出现在std::vector
代码中,因为您使用的是groups
,而groups
由于BackgroundGroupsHandler
无效而无效。The problem is most likely that the
BackgroundGroupsHandler
that you are callingaddBeat
on isNULL
or otherwise an invalid pointer. The problem shows up in thestd::vector
code because you are usinggroups
, which will be invalid due to theBackgroundGroupsHandler
being invalid.