Claire
Claire

Reputation: 97

Access violation reading location 0xFFFFFFFFFFFFFFFF

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.

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:

Unhandled exception at 0x00007FF6CB723D43 in myprogram.exe: 0xC0000005: Access violation reading location 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";

}

Upvotes: 3

Views: 62431

Answers (3)

Claire
Claire

Reputation: 97

Solved by @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;
    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";

}

Upvotes: 1

alex
alex

Reputation: 2482

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.

Then you should initialize parent & child in the second constructor as well (presumably with 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.

const unsigned int NUM_BONES

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

Upvotes: 4

wallycz
wallycz

Reputation: 308

Add

parent = NULL;
child = NULL;

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

Upvotes: 5

Related Questions