Adding a shared_ptr to my map throws read access violation

211 Views Asked by At

So I am working on building a game engine in C++, just for learning purposes. I am trying to follow a Entity Component System and I am having a problem when I add components to my Entity.

In my Entity class I want to hold all the components of that entity in: std::map<type_index, std::shared_ptr<Component>> components;

When I add a component I have this method in the .h file of my Entity class:

template<typename T, typename... TArgs> 
std::shared_ptr<T> addComponent(TArgs&&... mArgs) {
    std::shared_ptr<T> component(new T(std::forward<TArgs>(mArgs)...)); 
    component->entity = this;
    component->init();

    // This is where the error is being thrown 
    components.emplace(type_index(typeid(*component)), component); 
    return component;
}

in the same method I have also done some different things like trying to add components like components[std::type_index(typeid(*component))] = component; instead of using emplace and then I have also tried to std::move the component into the map.

My exact error is:

    Unhandled exception thrown: read access violation.
    **std::forward<std::_Tree_node<std::pair<std::type_index const ,std::shared_ptr<Component> >,void *> * &>**(...) returned 0x4. occurred
0

There are 0 best solutions below