🐛 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;
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;
out AObj: T): Boolean;
var
current: PCacheEntry;
current, prev: PCacheEntry;
begin
current := FFirst;
FLast := current;
prev := nil;
Result := False;
while (current <> nil) and (not Result) do
while current <> nil do
begin
if (current^.ID = AID) then
if current^.ID = AID then
begin
Result := True;
AObj := current^.Obj;
if current <> FFirst then
begin
FLast^.Next := current^.Next;
prev^.Next := current^.Next;
current^.Next := FFirst;
FFirst := current;
UpdateLast;
end;
Break;
end;
if (current^.Next <> nil) then
FLast := current;
prev := current;
current := current^.Next;
end;
end;