🐛 Fix cache last-entry corruption on hits

QueryID set the last-entry pointer to the moved node on every hit,
breaking the pointer-to-second-to-last invariant. StoreID then
orphaned tail entries, leaking cached objects and crashing on cache
destruction.
This commit is contained in:
2026-11-15 22:06:50 +01:00
parent 98bfbd7b11
commit 4108ed5900
+21 -7
View File
@@ -222,29 +222,43 @@ begin
Result := ACacheEntry <> nil; Result := ACacheEntry <> nil;
end; end;
procedure TCacheManager.UpdateLast;
begin
if FFirst = nil then
begin
FLast := nil;
Exit;
end;
FLast := FFirst;
if FLast^.Next = nil then Exit;
while FLast^.Next^.Next <> nil do
FLast := FLast^.Next;
end;
function TCacheManager.QueryID(const AID: Integer; function TCacheManager.QueryID(const AID: Integer;
out AObj: T): Boolean; out AObj: T): Boolean;
var var
current: PCacheEntry; current, prev: PCacheEntry;
begin begin
current := FFirst; current := FFirst;
FLast := current; prev := nil;
Result := False; Result := False;
while (current <> nil) and (not Result) do while current <> nil do
begin begin
if (current^.ID = AID) then if current^.ID = AID then
begin begin
Result := True; Result := True;
AObj := current^.Obj; AObj := current^.Obj;
if current <> FFirst then if current <> FFirst then
begin begin
FLast^.Next := current^.Next; prev^.Next := current^.Next;
current^.Next := FFirst; current^.Next := FFirst;
FFirst := current; FFirst := current;
UpdateLast;
end; end;
Break;
end; end;
if (current^.Next <> nil) then prev := current;
FLast := current;
current := current^.Next; current := current^.Next;
end; end;
end; end;