为什么在读回私有类变量时会得到EXC_BAD_ACCESS?

时间:2013-07-06 16:59:38

标签: c++ class runtime-error exc-bad-access

我有一个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 %f\n", width, height);
    _width = width;
    _height = height;
    printf("set _width to %f\n", _width);
}

float Rectangle::getWidth(float* width) const {
    printf("_w: %f\n", _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: %f\n", _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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  

我在行[...]上收到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();

这可能更干净,肯定不会导致这样的错误。一个好的经验法则是在可能的情况下永远不要使用指针。如果您需要修改函数内部的变量,只需使用引用。