为什么在读取私有类变量时会得到EXC_BAD_ACCESS
Why do I get an EXC_BAD_ACCESS when reading back a private class variable?
我有一个Rectangle
类,如下所示:
标题:
class Rectangle: public Polygon {
private:
float _width, _height;
public:
Rectangle(float width, float height);
float getWidth(float* width) const;
float getHeight(float* height) const;
bool isCollidingWith(Rectangle* other) const;
};
选定实施:
Rectangle::Rectangle(float width, float height) : Polygon(explodeRect(width, height, new struct vertex[4]), 4) {
printf("creating rect %f x %fn", width, height);
_width = width;
_height = height;
printf("set _width to %fn", _width);
}
float Rectangle::getWidth(float* width) const {
printf("_w: %fn", _width);
*width = _width;
return *width;
//return (*width = _width);
}
float Rectangle::getHeight(float* height) const {
return (*height = _height);
}
我初始化了Rectangle
类的一个实例,输出指示_width
变量被正确分配。然而,当我稍后尝试使用getWidth
方法读取变量时,我在以下行中得到了EXC_BAD_ACCESS
错误:
printf("_w: %fn", _width);
为什么我不能再读取这个变量了?对于_height
变量,我也遇到了同样的问题。
编辑:我还想注意的是,如果我跳过读取宽度,我会在尝试直接从对象读取公共变量时出错,例如,当我尝试用obj->x
读取其x位置时。
编辑2:这可能是因为对象是Rectangle
的子类的实例,而这个子类是在与Rectangle
不同的文件中定义的吗?我还在读取第三个文件中的值。
编辑3:下面是更多代码。
我正在尝试用OpenGL重新创建俄罗斯方块。在我的display
方法中,我有以下代码来绘制矩形:
if(fallingBlock != nullptr) {
printf("drawing falling block at (%f, %f)n", fallingBlock->x, fallingBlock->y);
draw(fallingBlock);
}
fallingBlock
被定义为我的文件顶部的全局变量:
Block* fallingBlock;
从我的main
,我调用了一个initVars
方法,该方法随后调用了startDroppingBlock
方法。这是:
void startDroppingBlock() {
Block* block = availableBlocks[random() % numAvailableBlocks].copy();
block->x = 0.5;
block->y = SCREEN_TOP;
block->dy = -0.01f;
//printf("copied block is at (%f, %f)n", block->x, block->y);
fallingBlock = block;
}
这是我的方块图绘制方法:
void draw(Block* obj) {
bool shape[3][3];
obj->getShape(shape);
//printf("got shape: {%d, %d, %d}, {%d, %d, %d}, {%d, %d, %d}n", shape[0][0], shape[0][1], shape[0][2], shape[1][0], shape[1][1], shape[1][2], shape[2][0], shape[2][1], shape[2][2]);
/*float pieceWidth;
obj->getWidth(&pieceWidth);
pieceWidth /= 3.0f;*/
float pieceWidth = obj->getWidth();
for(unsigned int i=0; i<3; i++) {
for(unsigned int j=0; j<3; j++) {
if(shape[i][j]) {
Square rect = Square(pieceWidth);
rect.x = obj->x + pieceWidth * j;
rect.y = obj->y + pieceWidth * i;
rect.color = obj->color;
draw(&rect);
}
}
}
}
我在[…]行收到EXC_BAD_ACCESS错误。为什么我不能再读取此变量?_height变量也有同样的问题。[稍后…]我已经尝试了
float pieceWidth; obj->getWidth(&pieceWidth);
和obj->getWidth(new float)
——实际错误在我读取_width
的那一行,甚至在我使用传入指针之前。[稍后…]我修改了getWidth和getHeight方法,只返回_width和_height。现在我在return _width;
在这种情况下,我看到您使用的是Rectangle*
指针作为obj->getWidth
,如果obj
不是有效指针,也可能导致错误的访问错误。
需要注意的是,我根本不太理解你的getter方法。它的简化版本(可能是标准)可能是:
float Rectangle::getWidth() const {
return _width;
}
唯一的区别是,当你使用时:
// float a;
// float b;
a = rect.getWidth(&b);
你现在可以做:
// float a;
// float b;
a = b = rect.getWidth();
这可能更干净,并且肯定不会造成这样的错误。一个很好的经验法则是尽可能不要使用指针。如果需要修改函数中的变量,只需使用引用即可。
- 使用来自 Excel VBA 的 C++ dll 时"Bad DLL calling convention" - 如何解决?
- C++17 编解码器在将标准::字符串转换为标准::字符串时抛出"bad conversion"
- 对于代理容器上的迭代器来说,"least bad implementation"可能是什么?
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 为什么 GetSystemMetrics (SM_CXVIRTUALSCREEN) 返回'bad'值?
- 将 lambda 传递给 STL 集时出错"bad function call"
- 新的 C++14 位分隔符处"Bad Character" Eclipse Oxygen 错误
- 使用 #define 被认为是"bad practice"吗?
- "400 Bad request"使用 OpenSSL BIO 进行请求时
- 解密文件AES_256_CBC返回"bad decrypt"错误
- 在C++流中,eof(),fail(),bad()和good()有什么区别
- make / gcc:"bad build"的可能原因是什么?
- 什么是地址0xbaddc0dedeadbead "Bad decode dead bead"
- 在多线程环境中,什么可能导致"bad file descriptor"?
- "std::vector"在调整大小时引发"bad allocation"异常
- 尝试使用 execp、dup2 和管道实现 shell,挂起或"bad file descriptor"
- OpenCV SVM 在火车上抛出异常,"Bad argument (There is only a single class)"
- 成员函数 bad() 的 std::ftsream 用于检查
- 使用 boost::asio 和 boost::thread 时"Bad file descriptor"
- OpenCV C++神经网络 predict() 函数抛出"Bad argument"错误