Search code examples
c++pointersrecursionassimp

random crashes with pointers and recursion


Im trying to map bones using recursive function. My goal is to run IterateBones on root bone to insert pointers to vector of pointers to Bones structure and every other children of children;

struct Bone
{
    int Id;
    std::vector<Bone*> BoneChild;
};
void IterateBones(aiNode* t,Bone* b,std::vector<Bone>& Bones, std::map<std::string,Bone*> Map)
{
        for(int g = 0 ; g<t->mNumChildren;g++)
        {
            Bone* Bon = Map.find(t->mChildren[g]->mName.data)->second;
            b->BoneChild.push_back(&Bones[Bon->Id]);
            IterateBones(t->mChildren[g],Bon,Bones,Map);
        }
    return;
}

aiNode* t is assimp pointer to node of bone,

Bone* b is pointer of currently iterated bone

std::vector& Bones is reference to vector of bones inside other class

std:: Map is mapped pointers to Bone structures by names in Bones vector

My problem is that the code works flawlessely 75% times, but 25% times the program crashes. i tried to debug with DBG, but it gave me segfault once, and other times it runs without any errors. Could anyone explain why is it happening and how i could prevent this, because i have simillar piece of code but it crashes most times

im using GC++ with mingw-w64


Solution

  • Problem was solved by @john and @EMarci15 in comments, it required to reserve Bones Vector

    Bones.reserve(500)