From 3c5401551d19ce9f60d1c090845df334debc40d5 Mon Sep 17 00:00:00 2001 From: garakmon Date: Thu, 6 Aug 2020 20:39:53 -0400 Subject: [PATCH] add Map class destructor to manage Event memory --- include/core/map.h | 4 ++++ src/core/editcommands.cpp | 1 - src/core/map.cpp | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/core/map.h b/include/core/map.h index 23e9be1c..867b5bc8 100644 --- a/include/core/map.h +++ b/include/core/map.h @@ -29,6 +29,7 @@ class Map : public QObject Q_OBJECT public: explicit Map(QObject *parent = nullptr); + ~Map(); public: QString name; @@ -92,6 +93,9 @@ public: void cacheBorder(); bool hasUnsavedChanges(); + // for memory management + QVector ownedEvents; + MapPixmapItem *mapItem = nullptr; void setMapItem(MapPixmapItem *item) { mapItem = item; } diff --git a/src/core/editcommands.cpp b/src/core/editcommands.cpp index ac214587..ba765908 100644 --- a/src/core/editcommands.cpp +++ b/src/core/editcommands.cpp @@ -324,7 +324,6 @@ EventShift::~EventShift() {} EventCreate::EventCreate(Editor *editor, Map *map, Event *event, QUndoCommand *parent) : QUndoCommand(parent) { - // setText("Create Event"); this->editor = editor; diff --git a/src/core/map.cpp b/src/core/map.cpp index 3d6f061d..3db6daeb 100644 --- a/src/core/map.cpp +++ b/src/core/map.cpp @@ -16,6 +16,14 @@ Map::Map(QObject *parent) : QObject(parent) editHistory.setClean(); } +Map::~Map() { + // delete all associated events + while (!ownedEvents.isEmpty()) { + Event *last = ownedEvents.takeLast(); + if (last) delete last; + } +} + void Map::setName(QString mapName) { name = mapName; constantName = mapConstantFromName(mapName); @@ -457,6 +465,7 @@ void Map::removeEvent(Event *event) { void Map::addEvent(Event *event) { events[event->get("event_group_type")].append(event); + if (!ownedEvents.contains(event)) ownedEvents.append(event); } bool Map::hasUnsavedChanges() {