C++ 访问冲突读取位置 0xFFFFFFFFFFFFFFFF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35441196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:31:02  来源:igfitidea点击:

Access violation reading location 0xFFFFFFFFFFFFFFFF

c++access-violation

提问by Claire

I'm attempting to use OpenGL to demonstrate hierarchical animation. In the early stage I'm trying to give my "bone" object a reference to its parent.

我正在尝试使用 OpenGL 来演示分层动画。在早期阶段,我试图给我的“骨骼”对象一个对其父对象的引用。

In my bone class, I can add a parent successfully however the issue is when I call hasParent(). It can't read this->parent and crashes with the following exception:

在我的骨骼课程中,我可以成功添加父级,但是问题是当我调用hasParent(). 它无法读取 this->parent 并崩溃,但出现以下异常:

Unhandled exception at 0x00007FF6CB723D43 in myprogram.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.**

myprogram.exe 中 0x00007FF6CB723D43 处的未处理异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF.**

Snippets from my bone class:

我的骨骼课程的片段:

void Bone::addParent(Bone *bone)
{
    this->parent = bone;
    assert(this->parent);
}

bool Bone::hasParent()
{
    assert(this->parent); //this line causes the error
    if (this->parent)
        return true;
    else return false;
}

glm::mat4 Bone::getBoneModel()
{
    glm::mat4 parentModel = glm::mat4(1.0);
    if (hasParent())
        parentModel = parent->getBoneModel();
    //boneModel = parentModel * boneModel;
    return boneModel;
}

Stripped down content of my main:

精简了我的主要内容:

#define NUMBONES 3
Bone bone[NUMBONES];

int main( void )
{
    //------------------ Create Bones --------------------------
    float y = 0.0f;
    for (int i = 0; i < NUMBONES; i++)
    {
        bone[i] = Bone(i, vec3(0, y, -30), vec3(0, 0, 0), vec3(0, 0, 0));
        y += 5.0f;
    }

    //----------------- Make relationships ----------------
    bone[0].isRoot = true;
    bone[0].addChild(&bone[1]);
    bone[0].addChild(&bone[2]);
    bone[1].addParent(&bone[0]);
    bone[1].addChild(&bone[2]);
    bone[2].addParent(&bone[1]);

    do{
        ModelMatrix = bone[1].getBoneModel();
    }
    return 0;
}

I find references and pointers difficult to get my head around so I'm hoping this is obvious to someone else!

我发现参考资料和指示很难理解,所以我希望这对其他人来说是显而易见的!

EDIT:

编辑:

My constructors:

我的构造函数:

Bone::Bone() {
    parent = NULL;
    child = NULL;
    boneID = 0;
    boneModel = glm::mat4(1.0);
}

Bone::Bone(int ID, glm::vec3 T, glm::vec3 R, glm::vec3 S)
{
    boneID = ID;
    isRoot = false;
    pos = T;

    //---------- set boneModel ------------------
    glm::mat4 RotationMatrix = glm::mat4(1.0);
    glm::mat4 TranslationMatrix = translate(glm::mat4(), pos);
    glm::mat4 ScalingMatrix = scale(glm::mat4(), glm::vec3(1.0f, 1.0f, 1.0f));
    boneModel = TranslationMatrix * RotationMatrix * ScalingMatrix;

    std::cout << "bone[" << boneID << "] created.\n";

}

回答by alex

You do not need

你不需要

assert(this->parent)

in the first line of Bone::hasParent(). Asserting something means you expect it always to be true. But then why do you have the function hasParent() in the first place? When this line is executed and there parent is not initialized, your program crashes. It should work if you remove the assert.

在 Bone::hasParent() 的第一行。断言某事意味着您期望它始终是真实的。但是为什么你首先有函数 hasParent() 呢?当执行此行并且未初始化父级时,您的程序将崩溃。如果您删除断言,它应该可以工作。

Then you should initialize parent & child in the second constructor as well (presumably with nullptr).

然后你也应该在第二个构造函数中初始化 parent & child (大概是 nullptr )。

Not related to your problem, but to improve your C++ style, you should not use #define's to define constants. Use const instead, e.g.

与您的问题无关,但为了改进您的 C++ 风格,您不应该使用 #define's 来定义常量。使用 const 代替,例如

const unsigned int NUM_BONES

See, e.g., "static const" vs "#define" vs "enum"

参见,例如,“静态常量”与“#define”与“枚举”

回答by wallycz

Add

添加

parent = NULL;
child = NULL;

to second constructor Bone(int ID, glm::vec3 T, glm::vec3 R, glm::vec3 S)

到第二个构造函数 Bone(int ID, glm::vec3 T, glm::vec3 R, glm::vec3 S)

回答by Claire

Solved by @drescherjm:

由@drescherjm 解决:

I needed to initialize 'parent = NULL' in Bone::Bone(int ID, glm::vec3 T, glm::vec3 R, glm::vec3 S)

我需要在 Bone::Bone(int ID, glm::vec3 T, glm::vec3 R, glm::vec3 S) 中初始化“parent = NULL”

Bone::Bone(int ID, glm::vec3 T, glm::vec3 R, glm::vec3 S)
{
    parent = NULL;
    boneID = ID;
    isRoot = false;
    pos = T;

    //---------- set boneModel ------------------
    glm::mat4 RotationMatrix = glm::mat4(1.0);
    glm::mat4 TranslationMatrix = translate(glm::mat4(), pos);
    glm::mat4 ScalingMatrix = scale(glm::mat4(), glm::vec3(1.0f, 1.0f, 1.0f));
    boneModel = TranslationMatrix * RotationMatrix * ScalingMatrix;

    std::cout << "bone[" << boneID << "] created.\n";

}