Skip to content

Commit

Permalink
Fixed stack frames being created when already cached.
Browse files Browse the repository at this point in the history
  • Loading branch information
iNinja committed Jan 29, 2025
1 parent bef98ca commit b680346
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,17 @@ public static StackFrame GetCurrentStackFrame(
{
// String is allocated, but it goes out of scope immediately after the call
string key = filePath + lineNumber;
StackFrame frame = CachedStackFrames.GetOrAdd(key, new StackFrame(skipFrames, true));
return frame;

// Try to get the stack frame from the cache,
// otherwise create a new one and add it to the cache
if (!CachedStackFrames.TryGetValue(key, out StackFrame? frame))
{
frame = new StackFrame(skipFrames, true);
CachedStackFrames.TryAdd(key, frame);
}

// By this point the frame cannot be null, but the compiler doesn't know that
return frame!;
}

// ConcurrentDictionary is thread-safe and only locks when adding a new item.
Expand Down

0 comments on commit b680346

Please sign in to comment.