Fix memory leaks in addNewMapToGroup()

This commit is contained in:
BigBahss 2021-02-18 17:41:36 -05:00 committed by huderlem
parent 02af128913
commit 0d3e2d90e6

View file

@ -99,18 +99,20 @@ QString Project::getProjectTitle() {
} }
void Project::clearMapCache() { void Project::clearMapCache() {
for (QString mapName : mapCache.keys()) { for (auto *map : mapCache.values()) {
Map *map = mapCache.take(mapName); if (map)
if (map) delete map; delete map;
} }
mapCache.clear();
emit mapCacheCleared(); emit mapCacheCleared();
} }
void Project::clearTilesetCache() { void Project::clearTilesetCache() {
for (QString tilesetName : tilesetCache.keys()) { for (auto *tileset : tilesetCache.values()) {
Tileset *tileset = tilesetCache.take(tilesetName); if (tileset)
if (tileset) delete tileset; delete tileset;
} }
tilesetCache.clear();
} }
Map* Project::loadMap(QString map_name) { Map* Project::loadMap(QString map_name) {
@ -1222,12 +1224,8 @@ void Project::writeBlockdata(QString path, const Blockdata &blockdata) {
} }
void Project::saveAllMaps() { void Project::saveAllMaps() {
QList<QString> keys = mapCache.keys(); for (auto *map : mapCache.values())
for (int i = 0; i < keys.length(); i++) {
QString key = keys.value(i);
Map* map = mapCache.value(key);
saveMap(map); saveMap(map);
}
} }
void Project::saveMap(Map *map) { void Project::saveMap(Map *map) {
@ -1866,26 +1864,24 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool
mapGroups.insert(mapName, groupNum); mapGroups.insert(mapName, groupNum);
groupedMapNames[groupNum].append(mapName); groupedMapNames[groupNum].append(mapName);
Map *map = new Map; newMap->isPersistedToFile = false;
map = newMap; newMap->setName(mapName);
map->isPersistedToFile = false; mapConstantsToMapNames.insert(newMap->constantName, newMap->name);
map->setName(mapName); mapNamesToMapConstants.insert(newMap->name, newMap->constantName);
mapConstantsToMapNames.insert(map->constantName, map->name);
mapNamesToMapConstants.insert(map->name, map->constantName);
if (!existingLayout) { if (!existingLayout) {
mapLayouts.insert(map->layoutId, map->layout); mapLayouts.insert(newMap->layoutId, newMap->layout);
mapLayoutsTable.append(map->layoutId); mapLayoutsTable.append(newMap->layoutId);
setNewMapBlockdata(map); setNewMapBlockdata(newMap);
setNewMapBorder(map); setNewMapBorder(newMap);
} }
loadMapTilesets(map); loadMapTilesets(newMap);
setNewMapEvents(map); setNewMapEvents(newMap);
setNewMapConnections(map); setNewMapConnections(newMap);
mapCache.insert(mapName, newMap);
return map; return newMap;
} }
QString Project::getNewMapName() { QString Project::getNewMapName() {