Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd string interpolation Null Reference Exception when debugging

Tags:

c#

.net-core

UPDATE TO THE UPDATE (Because this is just getting lost in bizarro world)

OK, I'm now totally blow away. I noticed in my update, I was missing the period after {b} on line 7. So, being a sticker for accuracy, I added it in.

!!!Now Line 7 also fails when you step into it!!!

What in the world????

So, now Program.cs with more crazy results...

try
{
    var a = "a";
    var b = "b";
    var r1 = "r";
    var r1x = $"r2x1={a}.{b}.{r1}";  // < Line 6 - F10 here will give you an exception
    var r1y = $"r2x1={a}.{b}" + r1;  // < Line 7 - F10 here will execute just fine!
    var r1z = $"r2x1={a}.{b}." + r1; // < Line 8 - F10 here will give you the exception!!!
    var r1a = $"r2x1={a}.{b}{r1}";   // < Line 9 - F10 here will give you an exception
    Console.WriteLine(r1x);
    
}
catch (NullReferenceException e)
{
    Console.WriteLine(e);
}

Update

I messed around some more with this, and the issue seems to be directly related to the string interpolation. In the updated Program.cs directly below, adding a breakpoint on Line 6 (var r1x = ...) will create an exception if you F10 over it, will happily run if you F5 it. However also having a breakpoint on line 7 (var r1y = ...) will happily allow you to F10 past it!

try
{
    var a = "a";
    var b = "b";
    var r1 = "r";
    var r1x = $"r2x1={a}.{b}.{r1}"; // < Line 6 - F10 here will give you an exception
    var r1y = $"r2x1={a}.{b}" + r1; // < Line 7 - F10 here will execute just fine!
    Console.WriteLine(r1x);
    
}
catch (NullReferenceException e)
{
    Console.WriteLine(e);
    
}

Original question below

Program.cs:

try
{
    var a = "a";
    var b = "b";
    var r1 = "r";
    var r2 = "r";
    r1 = $"r1={a}.{b}.{r1}";
    r2 = $"r2={a}.{b}.{r2}";
    Console.WriteLine(r1);
    Console.WriteLine(r2);

}
catch (NullReferenceException e)
{
    Console.WriteLine(e.StackTrace);
}

This silly bit of code will run just fine - if you're not doing line by line step-through with the debugger.

However placing a breakpoint on line 8 (r2 = ... ) will drop you into the catch with a NullReferenceException and

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Buffer.Memmove(Byte& dest, Byte& src, UIntPtr len)
   at System.Runtime.CompilerServices.DefaultInterpolatedStringHandler.AppendStringDirect(String value)
   at Program.<Main>$(String[] args) in /Users/rambler/tmp/Four Weeks/why/ConsoleApp1/Program.cs:line 8

Equally doing a breakpoint and line 7 will yield the same exception. Not having a breakpoint on either line will however result in an exception free execution.

I've tried several variations of the r1 = ... and r2 = ..., some which use string interpolation with r1 (or r2 respectively) and I get hard to pin down results.

All times though the code runs if I don't try to debug that particular line.

So far this has been on my Mac using both Rider and Visual Studio Code. Going to fire up Windows later on to see if I can replicate it there as well.

This issue came to light on a much much bigger app I'm writing - and I've been finally able to limit it down to just this.

.net 7.0.203

like image 265
Rachel Ambler Avatar asked Jul 01 '26 11:07

Rachel Ambler


1 Answers

This has been confirmed by others:

https://github.com/dotnet/runtime/issues/78991

like image 57
Rachel Ambler Avatar answered Jul 04 '26 03:07

Rachel Ambler