From 4108ed5900f3aff825bef12dbe52f3917b54226d Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sat, 14 Nov 2026 22:21:07 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20cache=20last-entry=20corru?= =?UTF-8?q?ption=20on=20hits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- UCacheManager.pas | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/UCacheManager.pas b/UCacheManager.pas index d6c77eb..210819f 100644 --- a/UCacheManager.pas +++ b/UCacheManager.pas @@ -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;