Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";

}
like image 848
Claire Avatar asked Feb 08 '23 17:02

Claire


1 Answers

Add

parent = NULL;
child = NULL;

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

like image 155
wallycz Avatar answered Feb 22 '23 11:02

wallycz