Skip to content

Fix ObjectDisposedException when opening Net OS Heap Alloc Stacks#2212

Merged
brianrob merged 3 commits intomainfrom
copilot/fix-2199
May 30, 2025
Merged

Fix ObjectDisposedException when opening Net OS Heap Alloc Stacks#2212
brianrob merged 3 commits intomainfrom
copilot/fix-2199

Conversation

Copy link
Contributor

Copilot AI commented May 27, 2025

Issue

When users try to open Net OS Heap Alloc Stacks view in PerfView, they encounter an ObjectDisposedException with the message:

Cannot access a disposed object. Object name: 'NativeSymbolModule'.

Root Cause

GetAllocationType method caches NativeSymbolModule objects in the loadedModules dictionary, but the SymbolReader class has its own limited cache (size 10) and disposes of modules when they're evicted from this cache. This creates a stale reference problem where the cached NativeSymbolModule objects in loadedModules are disposed but still referenced.

Fix

Changed the approach to cache PDB file paths rather than NativeSymbolModule objects:

  1. Modified the loadedModules dictionary from:

    var loadedModules = new Dictionary<TraceModuleFile, NativeSymbolModule>();

    to:

    var loadedModules = new Dictionary<TraceModuleFile, string>(); // Cache PDB paths, not NativeSymbolModule objects
  2. Updated the caching logic in GetAllocationType:

    // Get the PDB path from cache or find it
    if (!loadedModules.TryGetValue(module, out var pdbPath))
    {
        pdbPath = (module.PdbSignature != Guid.Empty
            ? symReader.FindSymbolFilePath(module.PdbName, module.PdbSignature, module.PdbAge, module.FilePath)
            : symReader.FindSymbolFilePathForModule(module.FilePath));
        loadedModules[module] = pdbPath; // Cache the path, not the module
    }
    
    // Get a fresh NativeSymbolModule for each lookup
    var symbolModule = (pdbPath != null) ? symReader.OpenNativeSymbolFile(pdbPath) : null;

This ensures we always get a fresh NativeSymbolModule from SymbolReader.OpenNativeSymbolFile each time, allowing the SymbolReader to properly manage its cache and object lifetimes.

Fixes #2199.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
Copilot AI changed the title [WIP] Opening Net OS Heap Alloc Stacks raises an ObjectDisposedException Fix ObjectDisposedException when opening Net OS Heap Alloc Stacks May 27, 2025
Copilot AI requested a review from brianrob May 27, 2025 19:39
@brianrob brianrob marked this pull request as ready for review May 27, 2025 21:50
Copy link
Member

@mconnew mconnew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@brianrob brianrob merged commit 9a3d9ae into main May 30, 2025
5 checks passed
@brianrob brianrob deleted the copilot/fix-2199 branch May 30, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Opening Net OS Heap Alloc Stacks raises an ObjectDisposedException

3 participants