I'm using an entity manager which basically updates and draws my container of entities. Like so:
void Draw(sf::RenderWindow *l_window) {
if (m_entities.empty()) return;
for (auto &itr : m_entities) {
itr.second->Draw(l_window);
}
}
I use a factory method that simply returns a pointer the the newly allocated memory:
template<typename T>
void RegisterEntity(const EntityType &l_type) {
m_entityFactory[l_type] = [this]() -> BaseEntity*
{
return new T();
};
}
Then, I add a new entity by just emplacing the pointer to a new container:
void AddEntity(const EntityType &l_type, const std::string &l_name, sf::Vector2f l_pos = sf::Vector2f(0,0)) {
auto itr = m_entityFactory.find(l_type);
if (itr == m_entityFactory.end()) return;
BaseEntity* entity = itr->second();
entity->m_id = m_idCounter;
if (entity->m_name == "") entity->m_name = l_name;
m_entities.emplace(m_idCounter, entity);
...
++m_idCounter;
}
Ultimately, I call the update and draw method on the main loop:
while (window.isOpen()) {
sf::Event evnt;
while (window.pollEvent(evnt)) {
if (evnt.type == sf::Event::Closed) {
window.close();
}
...
}
...
...
window.clear(sf::Color::Black);
entityManager.Draw(&window);
window.display();
time = clock.restart();
}
However, the "entityManager.Draw(&window)" is causing a memory leak. It only does that when it's drawing entities, which are only sf::RectangleShape. Even when I draw only one entity that happens. Here is the Base entity classe:
class BaseEntity {
friend class EntityManager;
protected:
...
sf::RectangleShape m_sprite;
...
public:
BaseEntity() {m_type = EntityType::Base;}
virtual ~BaseEntity() { }
virtual void Draw(sf::RenderWindow *l_window) = 0;
virtual void Update(sf::Time &l_time) = 0;
//getters
...
};
Each inheritable class has its own draw function. Like so:
class Player : public BaseEntity {
private:
public:
Player()
{
m_name = ""; m_health = 100; m_type = EntityType::Player;
m_sprite.setFillColor(sf::Color::Red);
m_sprite.setSize(sf::Vector2f(100, 100));
m_sprite.setOrigin(sf::Vector2f(m_sprite.getSize().x / 2, m_sprite.getSize().y / 2));
}
~Player() {};
void Draw(sf::RenderWindow *l_window) {
l_window->draw(*GetSprite());
}
...
};
Why is it leaking? Oh, and here are the containers that use to store all the entitites:
For the factory, I use std::unordered_map<EntityType, std::function<BaseEntity*()>> m_entityFactory;
And for the actual entities I use std::unordered_map<unsigned int, BaseEntity*> m_entities;
What's wrong here?