Skip to content

Commit

Permalink
Fix getBlock() memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
huderlem committed Feb 13, 2021
1 parent 3c2aa5d commit e9c4d5b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 173 deletions.
2 changes: 1 addition & 1 deletion include/core/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Map : public QObject
bool borderBlockChanged(int i, Blockdata * cache);
void cacheBlockdata();
void cacheCollision();
Block *getBlock(int x, int y);
bool getBlock(int x, int y, Block *out);
void setBlock(int x, int y, Block block, bool enableScriptCallback = false);
void floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation);
void _floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation);
Expand Down
50 changes: 25 additions & 25 deletions src/core/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,15 @@ void Map::setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata)
emit mapChanged(this);
}

Block* Map::getBlock(int x, int y) {
bool Map::getBlock(int x, int y, Block *out) {
if (layout->blockdata && layout->blockdata->blocks) {
if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
int i = y * getWidth() + x;
return new Block(layout->blockdata->blocks->value(i));
*out = layout->blockdata->blocks->value(i);
return true;
}
}
return nullptr;
return false;
}

void Map::setBlock(int x, int y, Block block, bool enableScriptCallback) {
Expand All @@ -395,55 +396,54 @@ void Map::_floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_
QPoint point = todo.takeAt(0);
x = point.x();
y = point.y();
Block *block = getBlock(x, y);
if (!block) {
Block block;
if (!getBlock(x, y, &block)) {
continue;
}

uint old_coll = block->collision;
uint old_elev = block->elevation;
uint old_coll = block.collision;
uint old_elev = block.elevation;
if (old_coll == collision && old_elev == elevation) {
continue;
}

block->collision = collision;
block->elevation = elevation;
setBlock(x, y, *block, true);
if ((block = getBlock(x + 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
block.collision = collision;
block.elevation = elevation;
setBlock(x, y, block, true);
if (getBlock(x + 1, y, &block) && block.collision == old_coll && block.elevation == old_elev) {
todo.append(QPoint(x + 1, y));
}
if ((block = getBlock(x - 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
if (getBlock(x - 1, y, &block) && block.collision == old_coll && block.elevation == old_elev) {
todo.append(QPoint(x - 1, y));
}
if ((block = getBlock(x, y + 1)) && block->collision == old_coll && block->elevation == old_elev) {
if (getBlock(x, y + 1, &block) && block.collision == old_coll && block.elevation == old_elev) {
todo.append(QPoint(x, y + 1));
}
if ((block = getBlock(x, y - 1)) && block->collision == old_coll && block->elevation == old_elev) {
if (getBlock(x, y - 1, &block) && block.collision == old_coll && block.elevation == old_elev) {
todo.append(QPoint(x, y - 1));
}
}
}

void Map::floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation) {
Block *block = getBlock(x, y);
if (block && (block->collision != collision || block->elevation != elevation)) {
Block block;;
if (getBlock(x, y, &block) && (block.collision != collision || block.elevation != elevation)) {
_floodFillCollisionElevation(x, y, collision, elevation);
}
}

void Map::magicFillCollisionElevation(int initialX, int initialY, uint16_t collision, uint16_t elevation) {
Block *block = getBlock(initialX, initialY);
if (block && (block->collision != collision || block->elevation != elevation)) {
uint old_coll = block->collision;
uint old_elev = block->elevation;
Block block;
if (getBlock(initialX, initialY, &block) && (block.collision != collision || block.elevation != elevation)) {
uint old_coll = block.collision;
uint old_elev = block.elevation;

for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
block = getBlock(x, y);
if (block && block->collision == old_coll && block->elevation == old_elev) {
block->collision = collision;
block->elevation = elevation;
setBlock(x, y, *block, true);
if (getBlock(x, y, &block) && block.collision == old_coll && block.elevation == old_elev) {
block.collision = collision;
block.elevation = elevation;
setBlock(x, y, block, true);
}
}
}
Expand Down
42 changes: 21 additions & 21 deletions src/mainwindow_scriptapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
QJSValue MainWindow::getBlock(int x, int y) {
if (!this->editor || !this->editor->map)
return QJSValue();
Block *block = this->editor->map->getBlock(x, y);
if (!block) {
Block block;
if (!this->editor->map->getBlock(x, y, &block)) {
return Scripting::fromBlock(Block());
}
return Scripting::fromBlock(*block);
return Scripting::fromBlock(block);
}

void MainWindow::tryRedrawMapArea(bool forceRedraw) {
Expand Down Expand Up @@ -51,65 +51,65 @@ void MainWindow::setBlocksFromSelection(int x, int y, bool forceRedraw, bool com
int MainWindow::getMetatileId(int x, int y) {
if (!this->editor || !this->editor->map)
return 0;
Block *block = this->editor->map->getBlock(x, y);
if (!block) {
Block block;
if (!this->editor->map->getBlock(x, y, &block)) {
return 0;
}
return block->tile;
return block.tile;
}

void MainWindow::setMetatileId(int x, int y, int metatileId, bool forceRedraw, bool commitChanges) {
if (!this->editor || !this->editor->map)
return;
Block *block = this->editor->map->getBlock(x, y);
if (!block) {
Block block;
if (!this->editor->map->getBlock(x, y, &block)) {
return;
}
this->editor->map->setBlock(x, y, Block(metatileId, block->collision, block->elevation));
this->editor->map->setBlock(x, y, Block(metatileId, block.collision, block.elevation));
this->tryCommitMapChanges(commitChanges);
this->tryRedrawMapArea(forceRedraw);
}

int MainWindow::getCollision(int x, int y) {
if (!this->editor || !this->editor->map)
return 0;
Block *block = this->editor->map->getBlock(x, y);
if (!block) {
Block block;
if (!this->editor->map->getBlock(x, y, &block)) {
return 0;
}
return block->collision;
return block.collision;
}

void MainWindow::setCollision(int x, int y, int collision, bool forceRedraw, bool commitChanges) {
if (!this->editor || !this->editor->map)
return;
Block *block = this->editor->map->getBlock(x, y);
if (!block) {
Block block;
if (!this->editor->map->getBlock(x, y, &block)) {
return;
}
this->editor->map->setBlock(x, y, Block(block->tile, collision, block->elevation));
this->editor->map->setBlock(x, y, Block(block.tile, collision, block.elevation));
this->tryCommitMapChanges(commitChanges);
this->tryRedrawMapArea(forceRedraw);
}

int MainWindow::getElevation(int x, int y) {
if (!this->editor || !this->editor->map)
return 0;
Block *block = this->editor->map->getBlock(x, y);
if (!block) {
Block block;
if (!this->editor->map->getBlock(x, y, &block)) {
return 0;
}
return block->elevation;
return block.elevation;
}

void MainWindow::setElevation(int x, int y, int elevation, bool forceRedraw, bool commitChanges) {
if (!this->editor || !this->editor->map)
return;
Block *block = this->editor->map->getBlock(x, y);
if (!block) {
Block block;
if (!this->editor->map->getBlock(x, y, &block)) {
return;
}
this->editor->map->setBlock(x, y, Block(block->tile, block->collision, elevation));
this->editor->map->setBlock(x, y, Block(block.tile, block.collision, elevation));
this->tryCommitMapChanges(commitChanges);
this->tryRedrawMapArea(forceRedraw);
}
Expand Down
22 changes: 12 additions & 10 deletions src/ui/collisionpixmapitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
this->lockedAxis = MapPixmapItem::Axis::None;
}

Block *block = map->getBlock(pos.x(), pos.y());
if (block) {
block->collision = this->movementPermissionsSelector->getSelectedCollision();
block->elevation = this->movementPermissionsSelector->getSelectedElevation();
map->setBlock(pos.x(), pos.y(), *block, true);
Block block;
if (map->getBlock(pos.x(), pos.y(), &block)) {
block.collision = this->movementPermissionsSelector->getSelectedCollision();
block.elevation = this->movementPermissionsSelector->getSelectedElevation();
map->setBlock(pos.x(), pos.y(), block, true);
}

Blockdata *newCollision = map->layout->blockdata->copy();
Expand Down Expand Up @@ -118,9 +118,9 @@ void CollisionPixmapItem::magicFill(QGraphicsSceneMouseEvent *event) {

void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
Block *block = map->getBlock(pos.x(), pos.y());
if (block) {
this->movementPermissionsSelector->select(block->collision, block->elevation);
Block block;
if (map->getBlock(pos.x(), pos.y(), &block)) {
this->movementPermissionsSelector->select(block.collision, block.elevation);
}
}

Expand All @@ -133,6 +133,8 @@ void CollisionPixmapItem::updateMovementPermissionSelection(QGraphicsSceneMouseE
if (pos.y() < 0) pos.setY(0);
if (pos.y() >= map->getHeight()) pos.setY(map->getHeight() - 1);

Block *block = map->getBlock(pos.x(), pos.y());
this->movementPermissionsSelector->select(block->collision, block->elevation);
Block block;
if (map->getBlock(pos.x(), pos.y(), &block)) {
this->movementPermissionsSelector->select(block.collision, block.elevation);
}
}
Loading

0 comments on commit e9c4d5b

Please sign in to comment.