Hello,
I’ve been following along a C++ book that uses SDL(1.6!?) but has updated source code provided for SDL2, which is what I follow from. It’s called “The Black Art of Multiplatform Game Programming” by Jazon YamamatoIt goes through the basics of SDL and then creates a mini engine.
Bạn đang xem: C++ std::list memory leaks (decremented before begin) ect.
I am towards the end of the book now and it goes through creating a side-scrolling map, where you can pick numbers 1-4 and drop various enemies on the map and they run back and forth to the edges of what platform they are on.
Xem thêm : Insulina y aumento de peso: Evita subir de peso
The problem is I get very weird behavior and lots of warnings which I tracked down to the Scene class. There is a part of the map where the NPC could be dropped and it would fall through causing a memory leak that would crash it. I decided to play around and I removed the decrementing part you can see comment below.
// Remove ‘removed’ nodes for (it = m_nodes.begin(); it != m_nodes.end(); it++) { if((*it)->isRemoved()) { SceneNode* oldNode = (*it); it-; // REMOVED THIS AND PROGRAM WORKS AS INTENDED, BUT FREEZES UP (STOP RESPONDING) ON CLOSE. removeNode(oldNode); } }
What errors I have come across messing with the class were related to “decrementing iterators before begin” and vice-a-versa. I came across a website specifically mentioning some of the faults of the std::list with memory leaks. When I remove that part it doesn’t drop through the map at that “special” spot and the NPC’s move back and forth as intended. But when closing the program it does the “not responding” crash. I wasn’t surprised that it caused that behavior, but I was just trying to tinker.
Xem thêm : Baltimore City Health Department
I am still rather new and was hoping some could tell me a solution. I wanted to mention that I previously had the for loops modernized, but decided to resort to the official source code in case I might have done something wrong. This is directly copied from the book, with the renaming of the class member variables with an “m_” prefix. So, unfortunately, it’s not an error on my end. Here is the source code for the Scene Class
Thanks, everyone!
#ifndef SCENE_H #define SCENE_H #include “vld.h” #include <list> #include “SceneNode.h” #include “Rectangle.h” class SceneNode; // Model a scene and manage it class Scene { private: std::list<SceneNode*> m_nodes; // holds all scene nodes public: Scene(); ~Scene(); void addNode(SceneNode* node); void removeNode(SceneNode* node); void update(); void draw(Rectangle* view, Graphics* gfx); std::list<SceneNode*>* getNodes(); }; #endif #include “Scene.h” Scene::Scene()= default; Scene::~Scene() = default; // Adds a node into the scene void Scene::addNode(SceneNode* node) { node->setScene(this); m_nodes.push_back(node); } // Removes desired node and deletes it. IT also removes every single node in the scene if nullptr is passed as argument void Scene::removeNode(SceneNode* node) { if(node != nullptr) { SceneNode* n = node; m_nodes.remove(node); delete n; n = nullptr; } else { while (!m_nodes.empty()); { removeNode(*m_nodes.begin()); } } } // Updates every single object from scene. Removes objects that are flagged for removal void Scene::update() { std::list<SceneNode*>::iterator it; // Remove ‘removed’ nodes for (it = m_nodes.begin(); it != m_nodes.end(); it++) { if((*it)->isRemoved()) { SceneNode* oldNode = (*it); } } // Update nodes for (it = m_nodes.begin(); it != m_nodes.end(); it++) { (*it)->update(); } } // View represents the portion of the scene that is to be drawn. Think of this as a camera that can scroll through the scene void Scene::draw(Rectangle* view, Graphics* gfx) { for (std::list<SceneNode*>::iterator it = m_nodes.begin(); it != m_nodes.end(); it++) { (*it)->draw(view, gfx); } } // Returns a pointer to the game object list std::list<SceneNode*>* Scene::getNodes() { return &m_nodes; }
Nguồn: https://buycookiesonline.eu
Danh mục: Info