I am getting a program crash from the following code:
// declaration in header:
std::vector<Animation> mAnimations; // animation objects
std::map<std::string, Animation*> mBoneAnimations; // map strings to animation objects from "mAnimations"
// source code in source file:
this->mBoneAnimations[bone_name] = &this->mAnimations.back();
// at different points, we modify the LAST element in animation
// this is where the problem occurs later on in the program:
Animation test = *(this->mBoneAnimations.at("Body")); // CRASH
this->mBoneAnimations[bone_name] = &this->mAnimations[this->mAnimations.size() - 1];
When you append to a std::vector, it sometimes has to reallocate. When it does that, it moves all of its elements to a new location in order to keep the elements in contiguous memory. This invalidates all pointers, references and iterators, so mBoneAnimations is storing dangling pointers.
So long as you only ever push/pop to the back/front, a quick fix would be to use std::deque instead. When you push_back to a deque and run out of space, instead of reallocating all the elements, it just adds a new block of elements, leaving the old ones where they were. (see section on iterator validity). Same for push front and pop.