I have a class Entity which has a destroy method used to delete it and free dynamically allocated memory that it has stored. It compiles without any errors, but whenever I use the destroy method crashes the program.Here is some of the code:
ECS.h (Entity class)
class Entity {public: //destroy is not defined here because I was getting errors from calling Manager before it was defined inline void destroy();};//definition of destroyvoid Entity::destroy() { manager->removeEntity(this);}
Manager class
class Manager {public: void removeEntity(Entity* e) { std::unique_ptr<Entity> ptr{ e }; //finds the position of Entity 'e' in the vector of entities std::vector<std::unique_ptr<Entity>>::iterator position = std::find(entities.begin(), entities.end(), ptr); if(static_cast<std::vector<std::unique_ptr<Entity>>::const_iterator>(position) != entities.end()) //deletes Entity 'e' from the vector of entities entities.erase(static_cast<std::vector<std::unique_ptr<Entity>>::const_iterator>(position)); }private: //vector containing unique_ptr's to all of the entities that Manager holds std::vector<std::unique_ptr<Entity>> entities;};
All of this works fine but anytime that I call the destroy function of an entity it immediately causes the program to crash.
Any idea why this might be happening?