Runtime: new GCMemoryInfo().Index throws NullReferenceException

Created on 1 Dec 2020  路  5Comments  路  Source: dotnet/runtime

Description

This program throws NullReferenceException:

```C#
using System;

class Program
{
static void Main()
{
Console.WriteLine(new GCMemoryInfo().Index);
}
}

I would expect it to write 0 instead. No exceptions are documented for the [GCMemoryInfo.Index](https://docs.microsoft.com/en-us/dotnet/api/system.gcmemoryinfo.index?view=net-5.0#System_GCMemoryInfo_Index) property.

### Configuration

.NET 5.0 on Windows 10 20H2 x64. I guess the exception would occur with all CoreCLR configurations; not sure about Mono.

### Regression?

No, System.GCMemoryInfo is a new API in .NET 5.0.

### Other information

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.GCMemoryInfo.get_Index()
at Program.Main() in C:\redacted\Program.cs:line 7
```

.NET Blog: The updated GetGCMemoryInfo API in .NET 5.0 and how it can help you

https://github.com/dotnet/runtime/issues/34648#issuecomment-627541970 suggested using public long PinnedHandlesCount => _info?.PinnedHandlesCount ?? 0; etc. but the properties were implemented in https://github.com/dotnet/runtime/pull/37879 without the null coalescing. https://github.com/dotnet/runtime/blob/cf258a14b70ad9069470a108f13765e0e5988f51/src/libraries/System.Private.CoreLib/src/System/GCMemoryInfo.cs#L150

Code like new GCMemoryInfo().Index seems unlikely to be part of real programs, but a program could have a GCMemoryInfo field in some class and try to read its properties before initializing it. The developer of such a program could avoid the problem by changing such fields to Nullable<GCMemoryInfo> and then checking for null.

area-GC-coreclr

Most helpful comment

IMO that's fine. It's an uninitialized value type where we don't guarantee that _default(T)_ is meaningful. Some other value types also throw NRE in this situation.

var e = new List<string>.Enumerator();
e.MoveNext(); // NullReferenceException

All 5 comments

I think this should have area-GC-coreclr like https://github.com/dotnet/runtime/issues/34648, rather than area-System.Reflection.

Tagging subscribers to this area: @dotnet/gc
See info in area-owners.md if you want to be subscribed.


Issue Details

Description

This program throws NullReferenceException:

```C#
using System;

class Program
{
static void Main()
{
Console.WriteLine(new GCMemoryInfo().Index);
}
}

I would expect it to write 0 instead. No exceptions are documented for the [GCMemoryInfo.Index](https://docs.microsoft.com/en-us/dotnet/api/system.gcmemoryinfo.index?view=net-5.0#System_GCMemoryInfo_Index) property.

### Configuration

.NET 5.0 on Windows 10 20H2 x64. I guess the exception would occur with all CoreCLR configurations; not sure about Mono.

### Regression?

No, System.GCMemoryInfo is a new API in .NET 5.0.

### Other information

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.GCMemoryInfo.get_Index()
at Program.Main() in C:\redacted\Program.cs:line 7
```

.NET Blog: The updated GetGCMemoryInfo API in .NET 5.0 and how it can help you

https://github.com/dotnet/runtime/issues/34648#issuecomment-627541970 suggested using public long PinnedHandlesCount => _info?.PinnedHandlesCount ?? 0; etc. but the properties were implemented in https://github.com/dotnet/runtime/pull/37879 without the null coalescing. https://github.com/dotnet/runtime/blob/cf258a14b70ad9069470a108f13765e0e5988f51/src/libraries/System.Private.CoreLib/src/System/GCMemoryInfo.cs#L150

Code like new GCMemoryInfo().Index seems unlikely to be part of real programs, but a program could have a GCMemoryInfo field in some class and try to read its properties before initializing it. The developer of such a program could avoid the problem by changing such fields to Nullable<GCMemoryInfo> and then checking for null.

Author: KalleOlaviNiemitalo
Assignees: -
Labels: `area-GC-coreclr`, `area-System.Reflection`, `untriaged`
Milestone: -

personally I would never use it that way 'cause you are asking for something that you don't have any info for and Index 0 is a piece of info. but I'll defer that to API folks to see if this is valid usage. @stephentoub, @GrabYourPitchforks, could you please comment?

BTW Index 0 means no GC has happened so we should not return 0 in this case.

IMO that's fine. It's an uninitialized value type where we don't guarantee that _default(T)_ is meaningful. Some other value types also throw NRE in this situation.

var e = new List<string>.Enumerator();
e.MoveNext(); // NullReferenceException
Was this page helpful?
0 / 5 - 0 ratings