Fix critical memory leaks, race conditions, and improve code quality
- Fix memory leaks in match3_gameplay.gd with proper queue_free() usage - Add comprehensive error handling and fallback mechanisms to SettingsManager - Resolve scene loading race conditions in GameManager with state protection - Remove problematic static variables from tile.gd, replace with instance-based approach - Consolidate duplicate debug menu classes into shared DebugMenuBase - Add input validation across all user input paths for security and stability
This commit is contained in:
@@ -24,13 +24,7 @@ var keyboard_navigation_enabled: bool = false
|
||||
func _ready():
|
||||
DebugManager.log_debug("Match3 _ready() started", "Match3")
|
||||
|
||||
# Set up initial gem pool
|
||||
var gem_indices = []
|
||||
for i in range(TILE_TYPES):
|
||||
gem_indices.append(i)
|
||||
|
||||
const TileScript = preload("res://scenes/game/gameplays/tile.gd")
|
||||
TileScript.set_active_gem_pool(gem_indices)
|
||||
# Gem pool will be set individually on each tile during creation
|
||||
|
||||
_calculate_grid_layout()
|
||||
_initialize_grid()
|
||||
@@ -58,14 +52,11 @@ func _calculate_grid_layout():
|
||||
)
|
||||
|
||||
func _initialize_grid():
|
||||
# Update gem pool BEFORE creating any tiles
|
||||
# Create gem pool for current tile types
|
||||
var gem_indices = []
|
||||
for i in range(TILE_TYPES):
|
||||
gem_indices.append(i)
|
||||
|
||||
const TileScript = preload("res://scenes/game/gameplays/tile.gd")
|
||||
TileScript.set_active_gem_pool(gem_indices)
|
||||
|
||||
for y in range(GRID_SIZE.y):
|
||||
grid.append([])
|
||||
for x in range(GRID_SIZE.x):
|
||||
@@ -74,9 +65,14 @@ func _initialize_grid():
|
||||
tile.position = tile_position
|
||||
tile.grid_position = Vector2i(x, y)
|
||||
add_child(tile)
|
||||
|
||||
# Set gem types for this tile
|
||||
tile.set_active_gem_types(gem_indices)
|
||||
|
||||
# Set tile type after adding to scene tree so sprite reference is available
|
||||
var new_type = randi() % TILE_TYPES
|
||||
tile.tile_type = new_type
|
||||
|
||||
# Connect tile signals
|
||||
tile.tile_selected.connect(_on_tile_selected)
|
||||
DebugManager.log_debug("Created tile at grid(%d,%d) world_pos(%s) with type %d" % [x, y, tile_position, new_type], "Match3")
|
||||
@@ -147,10 +143,13 @@ func _clear_matches():
|
||||
if to_clear.size() == 0:
|
||||
return
|
||||
|
||||
# Clear grid references first, then queue nodes for removal
|
||||
for tile in to_clear:
|
||||
grid[tile.grid_position.y][tile.grid_position.x] = null
|
||||
tile.queue_free()
|
||||
|
||||
# Wait one frame for nodes to be queued properly
|
||||
await get_tree().process_frame
|
||||
_drop_tiles()
|
||||
await get_tree().create_timer(0.2).timeout
|
||||
_fill_empty_cells()
|
||||
@@ -174,16 +173,29 @@ func _drop_tiles():
|
||||
moved = true
|
||||
|
||||
func _fill_empty_cells():
|
||||
# Create gem pool for current tile types
|
||||
var gem_indices = []
|
||||
for i in range(TILE_TYPES):
|
||||
gem_indices.append(i)
|
||||
|
||||
for y in range(GRID_SIZE.y):
|
||||
for x in range(GRID_SIZE.x):
|
||||
if not grid[y][x]:
|
||||
var tile = TILE_SCENE.instantiate()
|
||||
tile.grid_position = Vector2i(x, y)
|
||||
tile.tile_type = randi() % TILE_TYPES
|
||||
tile.position = grid_offset + Vector2(x, y) * tile_size
|
||||
grid[y][x] = tile
|
||||
add_child(tile)
|
||||
|
||||
# Set gem types for this tile
|
||||
tile.set_active_gem_types(gem_indices)
|
||||
|
||||
# Set random tile type
|
||||
tile.tile_type = randi() % TILE_TYPES
|
||||
|
||||
grid[y][x] = tile
|
||||
# Connect tile signals for new tiles
|
||||
tile.tile_selected.connect(_on_tile_selected)
|
||||
|
||||
# Fixed: Add recursion protection to prevent stack overflow
|
||||
await get_tree().create_timer(0.1).timeout
|
||||
var max_iterations = 10
|
||||
@@ -208,21 +220,21 @@ func regenerate_grid():
|
||||
if script_path == "res://scenes/game/gameplays/tile.gd":
|
||||
children_to_remove.append(child)
|
||||
|
||||
# Remove all found tile children
|
||||
for child in children_to_remove:
|
||||
child.free()
|
||||
|
||||
# Also clear grid array references
|
||||
# First clear grid array references to prevent access to nodes being freed
|
||||
for y in range(grid.size()):
|
||||
if grid[y] and grid[y] is Array:
|
||||
for x in range(grid[y].size()):
|
||||
# Set to null since we already freed the nodes above
|
||||
grid[y][x] = null
|
||||
|
||||
# Clear the grid array
|
||||
grid.clear()
|
||||
|
||||
# No need to wait for nodes to be freed since we used free()
|
||||
# Remove all found tile children using queue_free for safe cleanup
|
||||
for child in children_to_remove:
|
||||
child.queue_free()
|
||||
|
||||
# Wait for nodes to be properly freed from the scene tree
|
||||
await get_tree().process_frame
|
||||
|
||||
# Fixed: Recalculate grid layout before regenerating tiles
|
||||
_calculate_grid_layout()
|
||||
@@ -315,25 +327,37 @@ func _input(event: InputEvent) -> void:
|
||||
|
||||
|
||||
func _move_cursor(direction: Vector2i) -> void:
|
||||
# Input validation for direction vector
|
||||
if abs(direction.x) > 1 or abs(direction.y) > 1:
|
||||
DebugManager.log_error("Invalid cursor direction vector: " + str(direction), "Match3")
|
||||
return
|
||||
|
||||
if direction.x != 0 and direction.y != 0:
|
||||
DebugManager.log_error("Diagonal cursor movement not supported: " + str(direction), "Match3")
|
||||
return
|
||||
|
||||
var old_pos = cursor_position
|
||||
var new_pos = cursor_position + direction
|
||||
|
||||
# Bounds checking
|
||||
new_pos.x = clamp(new_pos.x, 0, GRID_SIZE.x - 1)
|
||||
new_pos.y = clamp(new_pos.y, 0, GRID_SIZE.y - 1)
|
||||
|
||||
if new_pos != cursor_position:
|
||||
# Validate old position before accessing grid
|
||||
if old_pos.x >= 0 and old_pos.y >= 0 and old_pos.x < GRID_SIZE.x and old_pos.y < GRID_SIZE.y:
|
||||
var old_tile = grid[cursor_position.y][cursor_position.x]
|
||||
if old_tile and not old_tile.is_selected:
|
||||
old_tile.is_highlighted = false
|
||||
|
||||
DebugManager.log_debug("Cursor moved from (%d,%d) to (%d,%d)" % [old_pos.x, old_pos.y, new_pos.x, new_pos.y], "Match3")
|
||||
|
||||
# Clear highlighting from old cursor position (only if not selected)
|
||||
var old_tile = grid[cursor_position.y][cursor_position.x]
|
||||
if old_tile and not old_tile.is_selected:
|
||||
old_tile.is_highlighted = false
|
||||
|
||||
cursor_position = new_pos
|
||||
|
||||
# Highlight new cursor position (only if not selected)
|
||||
var new_tile = grid[cursor_position.y][cursor_position.x]
|
||||
if new_tile and not new_tile.is_selected:
|
||||
new_tile.is_highlighted = true
|
||||
# Validate new position before accessing grid
|
||||
if cursor_position.x >= 0 and cursor_position.y >= 0 and cursor_position.x < GRID_SIZE.x and cursor_position.y < GRID_SIZE.y:
|
||||
var new_tile = grid[cursor_position.y][cursor_position.x]
|
||||
if new_tile and not new_tile.is_selected:
|
||||
new_tile.is_highlighted = true
|
||||
|
||||
func _select_tile_at_cursor() -> void:
|
||||
if cursor_position.x >= 0 and cursor_position.y >= 0 and cursor_position.x < GRID_SIZE.x and cursor_position.y < GRID_SIZE.y:
|
||||
|
||||
Reference in New Issue
Block a user