Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - PoppingNuts

Pages: [1]
1
General / Re: SFML Draw method leaking memory?
« on: October 23, 2020, 12:40:25 am »
It's not uncommon that reporting tools provide false positives, e.g. if they don't account for the destruction of some objects at application termination.

But if there really is an issue with SFML, then it should be reproducible in a few lines of code.
Given that you're doing manual memory management with raw pointers, I'd claim, that the leaks are originating somewhere from your own code. I highly recommend using unique_ptr (or if necessary shared_ptr) and not do any manual memory management.

Yes, I think you're right. I know  a lot about unique pointers, but when should I use shared?

2
General / SFML Draw method leaking memory?
« on: October 20, 2020, 05:16:57 am »
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?

Pages: [1]