Skip to content

Conversation

Copy link
Contributor

Copilot AI commented May 23, 2025

This PR fixes a null reference exception that can occur in the GenFragmentationPercent(Gens gen) method when HeapStats is null.

Issue

The GenFragmentationPercent method calls GenSizeAfterMB, which accesses properties on HeapStats without checking if it's null, causing a null reference exception when HeapStats is null.

Fix

Added a null check at the beginning of the GenSizeAfterMB method that returns double.NaN when HeapStats is null. This ensures that:

  1. No null reference exception is thrown
  2. NaN propagates through the calculations in GenFragmentationPercent
  3. The behavior matches the requirement specified in the issue comments

The change is minimal and preserves the existing code structure.

public double GenSizeAfterMB(Gens gen)
{
    if (HeapStats == null)
    {
        return double.NaN;
    }
    
    // Rest of the method remains unchanged
}

Fixes #2012.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • crl3.digicert.com
    • Triggering command: dotnet build (dns block)
  • ocsp.digicert.com
    • Triggering command: dotnet build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 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.

…ats null check

Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
Copilot AI changed the title [WIP] GenFragmentationPercent(Gen) sometimes throws a null ref exception Fix null reference exception in GenFragmentationPercent method May 23, 2025
Copilot AI requested a review from brianrob May 23, 2025 03:17
Copy link
Contributor

@mrsharm mrsharm left a comment

Choose a reason for hiding this comment

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

LGTM! Will highlight if this fails again.

@brianrob
Copy link
Member

Copilot Post-Mortem:

  • This one looks right on the first time around. No tweaks needed.

@brianrob brianrob marked this pull request as ready for review May 23, 2025 18:44
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 2cec03f into main May 27, 2025
5 checks passed
@brianrob brianrob deleted the copilot/fix-2012 branch May 27, 2025 15:36
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.

GenFragmentationPercent(Gen) sometimes throws a null ref exception

5 participants