奇怪的错误:EXC_BAD_ACCESS在我的课堂上
Strange errors: EXC_BAD_ACCESS in my class
这是我的代码:
typedef struct TItemSelector{
ItemSelectFrame* pItems[2];
} SItemSelector;
class item {
public:
void expMethod();
SItemSelector itemSelector_;
UILayerButton* startBtn_;
};
void item::expMethod(){
startBtn_ = new UILayerButton();
for (i = 0; i < 3; i++) {
itemSelector_.pItems[i] = new ItemSelectFrame();
}
startBtn_->callMethodA();
}
void UILayerButton::callMethodA()
{
this->callMethodB();
}
void UILayerButton::callMethodB()
{
}
this->callMethodB();
时,"EXC_BAD_ACCESS"被遮住了。
之后,我找到了一个解决方法:
class item {
public:
void expMethod();
SItemSelector itemSelector_;
SItemSelector itemSelector2_; // work around
UILayerButton* startBtn_;
};
然后一切都很顺利...我只是不知道发生了什么,但callMethodB()
只是一个空洞的方法,与它无关。
我正在使用Apple LLVM 3.1,默认设置。
更新:修复了我的代码。
在此代码中:
for (i = 0; i < 3; i++) {
itemSelector_.pItems[i] = new ItemSelectFrame();
}
您正在itemSelector_.pItems
末尾写入pItems
因为它是一个长度2
数组,但您正在编写3
元素。
然后,这将覆盖恰好在内存中itemSelector_
后立即出现的startBtn_
。这解释了您随后阅读现已损坏的startBtn_
时的错误。
要么更改环路终止测试,要么增加pItems
的长度。我无法判断哪一个是正确的解决方案,但显然您会知道。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为什么我的 std::ref 无法按预期工作?
- 如何修复我的 c++ 毕达哥拉斯三重查找器中的'access violation reading location'错误?
- 也许我对 [class.access]/7 的理解不正确,但是