Skip to content

Commit

Permalink
Merge pull request #88629 from groud/fix_runtime_update_mem_leak
Browse files Browse the repository at this point in the history
Fix a memory leak with TileMap runtime updates
  • Loading branch information
akien-mga committed Feb 22, 2024
2 parents cf20bd7 + 33485e6 commit de272ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 19 additions & 8 deletions scene/2d/tile_map_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,7 @@ void TileMapLayer::_build_runtime_update_tile_data() {
if (!forced_cleanup) {
if (tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update)) {
if (_runtime_update_tile_data_was_cleaned_up || dirty.flags[DIRTY_FLAGS_LAYER_GROUP_TILE_SET]) {
_runtime_update_needs_all_cells_cleaned_up = true;
for (KeyValue<Vector2i, CellData> &E : tile_map) {
_build_runtime_update_tile_data_for_cell(E.value);
}
Expand Down Expand Up @@ -1414,14 +1415,24 @@ void TileMapLayer::_build_runtime_update_tile_data_for_cell(CellData &r_cell_dat
}

void TileMapLayer::_clear_runtime_update_tile_data() {
for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();

// Clear the runtime tile data.
if (cell_data.runtime_tile_data_cache) {
memdelete(cell_data.runtime_tile_data_cache);
cell_data.runtime_tile_data_cache = nullptr;
if (_runtime_update_needs_all_cells_cleaned_up) {
for (KeyValue<Vector2i, CellData> &E : tile_map) {
_clear_runtime_update_tile_data_for_cell(E.value);
}
_runtime_update_needs_all_cells_cleaned_up = false;
} else {
for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &r_cell_data = *cell_data_list_element->self();
_clear_runtime_update_tile_data_for_cell(r_cell_data);
}
}
}

void TileMapLayer::_clear_runtime_update_tile_data_for_cell(CellData &r_cell_data) {
// Clear the runtime tile data.
if (r_cell_data.runtime_tile_data_cache) {
memdelete(r_cell_data.runtime_tile_data_cache);
r_cell_data.runtime_tile_data_cache = nullptr;
}
}

Expand Down Expand Up @@ -1632,7 +1643,7 @@ void TileMapLayer::_deferred_internal_update() {

void TileMapLayer::_internal_update() {
// Find TileData that need a runtime modification.
// This may add cells to the dirty list is a runtime modification has been notified.
// This may add cells to the dirty list if a runtime modification has been notified.
_build_runtime_update_tile_data();

// Update all subsystems.
Expand Down
2 changes: 2 additions & 0 deletions scene/2d/tile_map_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ class TileMapLayer : public Node2D {
bool _runtime_update_tile_data_was_cleaned_up = false;
void _build_runtime_update_tile_data();
void _build_runtime_update_tile_data_for_cell(CellData &r_cell_data, bool p_auto_add_to_dirty_list = false);
bool _runtime_update_needs_all_cells_cleaned_up = false;
void _clear_runtime_update_tile_data();
void _clear_runtime_update_tile_data_for_cell(CellData &r_cell_data);

// Per-system methods.
#ifdef DEBUG_ENABLED
Expand Down

0 comments on commit de272ff

Please sign in to comment.