🚨 Stop the range selection helper from leaking

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.
This commit is contained in:
2026-11-15 22:06:51 +01:00
parent a1a40b6f61
commit 997aa4991a
2 changed files with 36 additions and 38 deletions
+33 -35
View File
@@ -3,13 +3,13 @@
* *
* The contents of this file are subject to the terms of the * The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only * Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance * (the "License"). You may not use this file except in compliance with
* with the License. * the License.
* *
* You can obtain a copy of the license at * You can obtain a copy of the license at
* http://www.opensource.org/licenses/cddl1.php. * http://www.opensource.org/licenses/cddl1.php.
* See the License for the specific language governing permissions * See the License for the specific language governing permissions and
* and limitations under the License. * limitations under the License.
* *
* When distributing Covered Code, include this CDDL HEADER in each * When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at * file and include the License file at
@@ -43,28 +43,29 @@ uses
UfrmMain, UWorldItem, math; UfrmMain, UWorldItem, math;
type type
{ TRangeSelectionHelper } { TRangeSelectionHelper }
TRangeSelectionHelper = class TRangeSelectionHelper = class
constructor Create(ACallback: TSelectedRangeCallback); constructor Create(ACallback: TSelectedRangeCallback);
destructor Destroy; override;
protected protected
FCallback: TSelectedRangeCallback; FCallback: TSelectedRangeCallback;
FItem1: TWorldItem; FHasFirst: Boolean;
FItem2: TWorldItem; FX1, FY1: Word;
procedure TileSelected(AWorldItem: TWorldItem); procedure TileSelected(AWorldItem: TWorldItem);
procedure Finish; procedure Finish(AX2, AY2: Word);
public public
procedure Run; procedure Run;
end; end;
procedure SelectRange(ACallback: TSelectedRangeCallback);
var var
helper: TRangeSelectionHelper; ActiveHelper: TRangeSelectionHelper = nil;
procedure SelectRange(ACallback: TSelectedRangeCallback);
begin begin
helper := TRangeSelectionHelper.Create(ACallback); FreeAndNil(ActiveHelper);
helper.Run; ActiveHelper := TRangeSelectionHelper.Create(ACallback);
//Cleanup will follow asynchroneously ActiveHelper.Run;
end; end;
{ TRangeSelectionHelper } { TRangeSelectionHelper }
@@ -72,42 +73,39 @@ end;
constructor TRangeSelectionHelper.Create(ACallback: TSelectedRangeCallback); constructor TRangeSelectionHelper.Create(ACallback: TSelectedRangeCallback);
begin begin
FCallback := ACallback; FCallback := ACallback;
FItem1 := nil; FHasFirst := False;
FItem2 := nil; end;
destructor TRangeSelectionHelper.Destroy;
begin
if frmMain <> nil then
frmMain.UnregisterSelectionListener(@TileSelected);
inherited Destroy;
end; end;
procedure TRangeSelectionHelper.TileSelected(AWorldItem: TWorldItem); procedure TRangeSelectionHelper.TileSelected(AWorldItem: TWorldItem);
begin begin
if FItem1 = nil then if not FHasFirst then
FItem1 := AWorldItem
else if FItem2 = nil then
begin begin
FItem2 := AWorldItem; FHasFirst := True;
Finish; FX1 := AWorldItem.X;
end; FY1 := AWorldItem.Y;
end else
Finish(AWorldItem.X, AWorldItem.Y);
end; end;
procedure TRangeSelectionHelper.Finish; procedure TRangeSelectionHelper.Finish(AX2, AY2: Word);
var
minX, minY: Word;
maxX, maxY: Word;
begin begin
frmMain.UnregisterSelectionListener(@TileSelected); FCallback(Min(FX1, AX2), Min(FY1, AY2), Max(FX1, AX2), Max(FY1, AY2));
minX := Min(FItem1.X, FItem2.X); if ActiveHelper = Self then
minY := Min(FItem1.Y, FItem2.Y); ActiveHelper := nil;
maxX := Max(FItem1.X, FItem2.X); Free;
maxY := Max(FItem1.Y, FItem2.Y);
FCallback(minX, minY, maxX, maxY);
Free; //We use this class only once, so it can cleanup after itself
end; end;
procedure TRangeSelectionHelper.Run; procedure TRangeSelectionHelper.Run;
begin begin
//TODO show indicator and option to cancel
//TODO keep track of instance (global variable maybe?)
frmMain.RegisterSelectionListener(@TileSelected); frmMain.RegisterSelectionListener(@TileSelected);
frmMain.SwitchToSelection; frmMain.SwitchToSelection;
end; end;
end. end.
+3 -3
View File
@@ -729,7 +729,7 @@ var
tileX, tileY, newX, newY: Word; tileX, tileY, newX, newY: Word;
targetBlocks: TBlockInfoList; targetBlocks: TBlockInfoList;
targetTile: TWorldItem; targetTile: TWorldItem;
selectionListener: TSelectionListener; listenerIndex: Integer;
begin begin
Logger.EnterMethod([lcClient, lcDebug], 'MouseUp'); Logger.EnterMethod([lcClient, lcDebug], 'MouseUp');
if Button <> mbLeft then if Button <> mbLeft then
@@ -757,9 +757,9 @@ begin
mnuGrabTileIDClick(nil); mnuGrabTileIDClick(nil);
end; end;
for selectionListener in FSelectionListeners do for listenerIndex := FSelectionListeners.Count - 1 downto 0 do
begin begin
selectionListener(CurrentTile); FSelectionListeners[listenerIndex](CurrentTile);
end; end;
end; end;