From 997aa4991a0465b7403096c0348c2bf748d599be Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 15 Nov 2026 09:22:35 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20Stop=20the=20range=20selection?= =?UTF-8?q?=20helper=20from=20leaking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Abandoning a selection after the first click leaked the helper and kept it registered for tile clicks. The helper now tracks the active instance, copies coordinates instead of holding tile references that can be evicted, and the listener dispatch tolerates removal during iteration. --- Client/USelectionHelper.pas | 68 ++++++++++++++++++------------------- Client/UfrmMain.pas | 6 ++-- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/Client/USelectionHelper.pas b/Client/USelectionHelper.pas index 5b81ee1..dc3ecd0 100644 --- a/Client/USelectionHelper.pas +++ b/Client/USelectionHelper.pas @@ -3,13 +3,13 @@ * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only - * (the "License"). You may not use this file except in compliance - * with the License. + * (the "License"). You may not use this file except in compliance with + * the License. * * You can obtain a copy of the license at * http://www.opensource.org/licenses/cddl1.php. - * See the License for the specific language governing permissions - * and limitations under the License. + * See the License for the specific language governing permissions and + * limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at @@ -43,28 +43,29 @@ uses UfrmMain, UWorldItem, math; type - { TRangeSelectionHelper } TRangeSelectionHelper = class constructor Create(ACallback: TSelectedRangeCallback); + destructor Destroy; override; protected FCallback: TSelectedRangeCallback; - FItem1: TWorldItem; - FItem2: TWorldItem; + FHasFirst: Boolean; + FX1, FY1: Word; procedure TileSelected(AWorldItem: TWorldItem); - procedure Finish; + procedure Finish(AX2, AY2: Word); public procedure Run; end; -procedure SelectRange(ACallback: TSelectedRangeCallback); var - helper: TRangeSelectionHelper; + ActiveHelper: TRangeSelectionHelper = nil; + +procedure SelectRange(ACallback: TSelectedRangeCallback); begin - helper := TRangeSelectionHelper.Create(ACallback); - helper.Run; - //Cleanup will follow asynchroneously + FreeAndNil(ActiveHelper); + ActiveHelper := TRangeSelectionHelper.Create(ACallback); + ActiveHelper.Run; end; { TRangeSelectionHelper } @@ -72,42 +73,39 @@ end; constructor TRangeSelectionHelper.Create(ACallback: TSelectedRangeCallback); begin FCallback := ACallback; - FItem1 := nil; - FItem2 := nil; + FHasFirst := False; +end; + +destructor TRangeSelectionHelper.Destroy; +begin + if frmMain <> nil then + frmMain.UnregisterSelectionListener(@TileSelected); + inherited Destroy; end; procedure TRangeSelectionHelper.TileSelected(AWorldItem: TWorldItem); begin - if FItem1 = nil then - FItem1 := AWorldItem - else if FItem2 = nil then + if not FHasFirst then begin - FItem2 := AWorldItem; - Finish; - end; + FHasFirst := True; + FX1 := AWorldItem.X; + FY1 := AWorldItem.Y; + end else + Finish(AWorldItem.X, AWorldItem.Y); end; -procedure TRangeSelectionHelper.Finish; -var - minX, minY: Word; - maxX, maxY: Word; +procedure TRangeSelectionHelper.Finish(AX2, AY2: Word); begin - frmMain.UnregisterSelectionListener(@TileSelected); - minX := Min(FItem1.X, FItem2.X); - minY := Min(FItem1.Y, FItem2.Y); - maxX := Max(FItem1.X, FItem2.X); - maxY := Max(FItem1.Y, FItem2.Y); - FCallback(minX, minY, maxX, maxY); - Free; //We use this class only once, so it can cleanup after itself + FCallback(Min(FX1, AX2), Min(FY1, AY2), Max(FX1, AX2), Max(FY1, AY2)); + if ActiveHelper = Self then + ActiveHelper := nil; + Free; end; procedure TRangeSelectionHelper.Run; begin - //TODO show indicator and option to cancel - //TODO keep track of instance (global variable maybe?) frmMain.RegisterSelectionListener(@TileSelected); frmMain.SwitchToSelection; end; end. - diff --git a/Client/UfrmMain.pas b/Client/UfrmMain.pas index 0cdef58..3f3be90 100644 --- a/Client/UfrmMain.pas +++ b/Client/UfrmMain.pas @@ -729,7 +729,7 @@ var tileX, tileY, newX, newY: Word; targetBlocks: TBlockInfoList; targetTile: TWorldItem; - selectionListener: TSelectionListener; + listenerIndex: Integer; begin Logger.EnterMethod([lcClient, lcDebug], 'MouseUp'); if Button <> mbLeft then @@ -757,9 +757,9 @@ begin mnuGrabTileIDClick(nil); end; - for selectionListener in FSelectionListeners do + for listenerIndex := FSelectionListeners.Count - 1 downto 0 do begin - selectionListener(CurrentTile); + FSelectionListeners[listenerIndex](CurrentTile); end; end;