mapcreator git · master
SDL3 2.5D game and engine using assets, with map editor
C++ 99.3%git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gzFix use after free object
src/objects/object.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/objects/object.cpp b/src/objects/object.cpp index 2e15682..adce898 100644 --- a/src/objects/object.cpp +++ b/src/objects/object.cpp @@ -17,21 +17,15 @@ Object::~Object() { size_t m_id = static_cast<size_t>((y+world_tile_position.y)*Map::size.x+(x+world_tile_position.x)); if (m_id >= static_cast<size_t>(Map::size.x*Map::size.y)) continue; - std::vector<TileObject>& objects = Map::tile_map[m_id].objects; + std::vector<TileObject>& t_objects = Map::tile_map[m_id].objects; - for (auto it = objects.begin(); it != objects.end(); ) { + for (auto it = t_objects.begin(); it != t_objects.end(); ) { if (it->object_id == id) - it = objects.erase(it); + it = t_objects.erase(it); else ++it; } } } - for (auto it = objects.begin(); it != objects.end(); ++it) { - if (*it == this) { - objects.erase(it); - break; - } - } }; const char* Object::to_String(ObjectType type) { @@ -75,6 +69,7 @@ Object::get_ObjectByWorldPositionAndTilesetObjectID(SDL_Point position, size_t t Object* Object::get_ObjectById(size_t id) { + //std::cout << "GETTT " << id << std::endl; for (Object* object : Object::objects) if (object->id == id) return object; return nullptr; @@ -314,7 +309,6 @@ Object::Remove(Object* obj) { for (int x = start_x; x < end_x; ++x) { size_t index = static_cast<size_t>(y * Map::size.x + x); if (index >= Map::tile_map.size()) continue; - std::cout << "xm " << x << " " << y << std::endl; auto& tile_objects = Map::tile_map[index].objects; @@ -326,5 +320,13 @@ Object::Remove(Object* obj) { } } + Object::objects.erase( + std::remove(Object::objects.begin(), + Object::objects.end(), + obj), + Object::objects.end() + ); + + delete obj; }