From 8187b2d4bbaace08cb43c4b59351fab9eb6edf0d Mon Sep 17 00:00:00 2001 From: BigBahss Date: Tue, 16 Feb 2021 12:14:27 -0500 Subject: [PATCH] Plug leaks in Metatile --- include/core/metatile.h | 3 ++- src/core/metatile.cpp | 28 +++++++++++++++++++--------- src/core/metatileparser.cpp | 4 ++-- src/core/tileset.cpp | 3 +-- src/mainwindow.cpp | 16 ++++++++-------- src/project.cpp | 4 ++-- src/ui/imageproviders.cpp | 4 ++-- src/ui/metatilelayersitem.cpp | 2 +- src/ui/tileseteditor.cpp | 18 +++++++++--------- 9 files changed, 46 insertions(+), 36 deletions(-) diff --git a/include/core/metatile.h b/include/core/metatile.h index c016ae7c..561a747e 100644 --- a/include/core/metatile.h +++ b/include/core/metatile.h @@ -11,8 +11,9 @@ class Metatile { public: Metatile(); + Metatile(const Metatile &other); public: - QList *tiles = nullptr; + QList tiles; uint16_t behavior; // 8 bits RSE, 9 bits FRLG uint8_t layerType; uint8_t encounterType; // FRLG only diff --git a/src/core/metatile.cpp b/src/core/metatile.cpp index e870dcad..7301eb12 100644 --- a/src/core/metatile.cpp +++ b/src/core/metatile.cpp @@ -2,10 +2,21 @@ #include "tileset.h" #include "project.h" -Metatile::Metatile() -{ - tiles = new QList; -} +Metatile::Metatile() : + behavior(0), + layerType(0), + encounterType(0), + terrainType(0) +{ } + +Metatile::Metatile(const Metatile &other) : + tiles(other.tiles), + behavior(other.behavior), + layerType(other.layerType), + encounterType(other.encounterType), + terrainType(other.terrainType), + label(other.label) +{ } Metatile* Metatile::copy() { Metatile *copy = new Metatile; @@ -13,10 +24,9 @@ Metatile* Metatile::copy() { copy->layerType = this->layerType; copy->encounterType = this->encounterType; copy->terrainType = this->terrainType; - copy->tiles = new QList; copy->label = this->label; - for (Tile tile : *this->tiles) { - copy->tiles->append(tile); + for (const Tile &tile : this->tiles) { + copy->tiles.append(tile); } return copy; } @@ -27,8 +37,8 @@ void Metatile::copyInPlace(Metatile *other) { this->encounterType = other->encounterType; this->terrainType = other->terrainType; this->label = other->label; - for (int i = 0; i < this->tiles->length(); i++) { - (*this->tiles)[i] = other->tiles->at(i); + for (int i = 0; i < this->tiles.length(); i++) { + this->tiles[i] = other->tiles.at(i); } } diff --git a/src/core/metatileparser.cpp b/src/core/metatileparser.cpp index 7c52c187..2fef6dc0 100644 --- a/src/core/metatileparser.cpp +++ b/src/core/metatileparser.cpp @@ -75,14 +75,14 @@ QList *MetatileParser::parse(QString filepath, bool *error, bool prim QList *metatiles = new QList(); for (int i = 0; i < numMetatiles; i++) { Metatile *metatile = new Metatile(); - QList *tiles = new QList(); + QList tiles; for (int j = 0; j < 8; j++) { int metatileOffset = 4 + i * metatileSize + j * 2; uint16_t word = static_cast( static_cast(in.at(metatileOffset)) | (static_cast(in.at(metatileOffset + 1)) << 8)); Tile tile(word & 0x3ff, (word >> 10) & 1, (word >> 11) & 1, (word >> 12) & 0xf); - tiles->append(tile); + tiles.append(tile); } int attrOffset = 4 + (numMetatiles * metatileSize) + (i * attrSize); diff --git a/src/core/tileset.cpp b/src/core/tileset.cpp index b3c81bc5..0ec18e0b 100644 --- a/src/core/tileset.cpp +++ b/src/core/tileset.cpp @@ -71,8 +71,7 @@ Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *seco if (!tileset || !tileset->metatiles) { return nullptr; } - Metatile *metatile = tileset->metatiles->value(local_index, nullptr); - return metatile; + return tileset->metatiles->value(local_index, nullptr); } bool Tileset::metatileIsValid(uint16_t metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 75f4494c..5d1740fc 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1219,16 +1219,16 @@ void MainWindow::on_actionNew_Tileset_triggered() { int tilesPerMetatile = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; Metatile *mt = new Metatile(); for(int j = 0; j < tilesPerMetatile; ++j){ - Tile *tile = new Tile(); + Tile tile; //Create a checkerboard-style dummy tileset if(((i / 8) % 2) == 0) - tile->tile = ((i % 2) == 0) ? 1 : 2; + tile.tile = ((i % 2) == 0) ? 1 : 2; else - tile->tile = ((i % 2) == 1) ? 1 : 2; - tile->xflip = false; - tile->yflip = false; - tile->palette = 0; - mt->tiles->append(*tile); + tile.tile = ((i % 2) == 1) ? 1 : 2; + tile.xflip = false; + tile.yflip = false; + tile.palette = 0; + mt->tiles.append(tile); } mt->behavior = 0; mt->layerType = 0; @@ -1239,7 +1239,7 @@ void MainWindow::on_actionNew_Tileset_triggered() { } newSet->palettes = new QList>(); newSet->palettePreviews = new QList>(); - newSet->palettePaths = *new QList(); + newSet->palettePaths.clear(); for(int i = 0; i < 16; ++i) { QList *currentPal = new QList(); for(int i = 0; i < 16;++i) { diff --git a/src/project.cpp b/src/project.cpp index 19df02a9..710e4edc 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1088,7 +1088,7 @@ void Project::saveTilesetMetatiles(Tileset *tileset) { for (Metatile *metatile : *tileset->metatiles) { int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; for (int i = 0; i < numTiles; i++) { - Tile tile = metatile->tiles->at(i); + Tile tile = metatile->tiles.at(i); uint16_t value = static_cast((tile.tile & 0x3ff) | ((tile.xflip & 1) << 10) | ((tile.yflip & 1) << 11) @@ -1626,7 +1626,7 @@ void Project::loadTilesetMetatiles(Tileset* tileset) { tile.xflip = (word >> 10) & 1; tile.yflip = (word >> 11) & 1; tile.palette = (word >> 12) & 0xf; - metatile->tiles->append(tile); + metatile->tiles.append(tile); } metatiles->append(metatile); } diff --git a/src/ui/imageproviders.cpp b/src/ui/imageproviders.cpp index a9d7cf29..4c37206e 100644 --- a/src/ui/imageproviders.cpp +++ b/src/ui/imageproviders.cpp @@ -26,7 +26,7 @@ QImage getMetatileImage( metatile_image.fill(Qt::black); Metatile* metatile = Tileset::getMetatile(tile, primaryTileset, secondaryTileset); - if (!metatile || !metatile->tiles) { + if (!metatile) { metatile_image.fill(Qt::magenta); return metatile_image; } @@ -46,7 +46,7 @@ QImage getMetatileImage( for (int x = 0; x < 2; x++) { int l = layerOrder.size() >= numLayers ? layerOrder[layer] : layer; int bottomLayer = layerOrder.size() >= numLayers ? layerOrder[0] : 0; - Tile tile_ = metatile->tiles->value((y * 2) + x + (l * 4)); + Tile tile_ = metatile->tiles.value((y * 2) + x + (l * 4)); QImage tile_image = getTileImage(tile_.tile, primaryTileset, secondaryTileset); if (tile_image.isNull()) { // Some metatiles specify tiles that are outside the valid range. diff --git a/src/ui/metatilelayersitem.cpp b/src/ui/metatilelayersitem.cpp index 9883c4d5..2f5f8b80 100644 --- a/src/ui/metatilelayersitem.cpp +++ b/src/ui/metatilelayersitem.cpp @@ -25,7 +25,7 @@ void MetatileLayersItem::draw() { QPainter painter(&pixmap); int numTiles = isTripleLayerMetatile ? 12 : 8; for (int i = 0; i < numTiles; i++) { - Tile tile = this->metatile->tiles->at(i); + Tile tile = this->metatile->tiles.at(i); QImage tileImage = getPalettedTileImage(tile.tile, this->primaryTileset, this->secondaryTileset, tile.palette, true) .mirrored(tile.xflip, tile.yflip) .scaled(16, 16); diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 2729fd37..9048f5a8 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -361,7 +361,7 @@ void TilesetEditor::onSelectedTilesChanged() { } void TilesetEditor::onMetatileLayerTileChanged(int x, int y) { - const QList tileCoords = QList{ + static const QList tileCoords = QList{ QPoint(0, 0), QPoint(1, 0), QPoint(0, 1), @@ -387,11 +387,11 @@ void TilesetEditor::onMetatileLayerTileChanged(int x, int y) { if (tileIndex < maxTileIndex && tileCoords.at(tileIndex).x() >= x && tileCoords.at(tileIndex).y() >= y){ - Tile *tile = &(*this->metatile->tiles)[tileIndex]; - tile->tile = tiles.at(selectedTileIndex).tile; - tile->xflip = tiles.at(selectedTileIndex).xflip; - tile->yflip = tiles.at(selectedTileIndex).yflip; - tile->palette = tiles.at(selectedTileIndex).palette; + Tile &tile = this->metatile->tiles[tileIndex]; + tile.tile = tiles.at(selectedTileIndex).tile; + tile.xflip = tiles.at(selectedTileIndex).xflip; + tile.yflip = tiles.at(selectedTileIndex).yflip; + tile.palette = tiles.at(selectedTileIndex).palette; } selectedTileIndex++; } @@ -416,7 +416,7 @@ void TilesetEditor::onMetatileLayerSelectionChanged(QPoint selectionOrigin, int for (int i = 0; i < width; i++) { int tileIndex = ((x + i) / 2 * 4) + ((y + j) * 2) + ((x + i) % 2); if (tileIndex < maxTileIndex) { - tiles.append(this->metatile->tiles->at(tileIndex)); + tiles.append(this->metatile->tiles.at(tileIndex)); tileIdxs.append(tileIndex); } } @@ -740,7 +740,7 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered() metatile->encounterType = 0; metatile->terrainType = 0; for (int i = 0; i < numTiles; i++) { - metatile->tiles->append(tile); + metatile->tiles.append(tile); } this->primaryTileset->metatiles->append(metatile); } @@ -760,7 +760,7 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered() metatile->encounterType = 0; metatile->terrainType = 0; for (int i = 0; i < numTiles; i++) { - metatile->tiles->append(tile); + metatile->tiles.append(tile); } this->secondaryTileset->metatiles->append(metatile); }