runtime: NullReferenceException when you use the short format of string.format $"{sample}" when debugging .NET 7 on x64 macOS

This issue has been moved from a ticket on Developer Community.


[severity:I’m unable to use this version] [regression] [worked-in:net 6] When you use the short format $“{sample}” to concatenate/format a string with variables you get this if is inside a try/catch

I have this piece of code:

[Fact]
public void NullReferenceException()
{
    var cname = "component";
    var callerName = "caller";
    var message = "message";

    string text = null;
    string textFormat = null;
    string textNRE = null;

    textNRE = $"{cname}.{callerName}: {message}"; // this works fine ;0

    try
    {
        text = cname + "." + callerName + ": " + message;
        textFormat = string.Format("{0}.{1}: {2}", cname, callerName, message);
        textNRE = $"{cname}.{callerName}: {message}"; // this throws the NullReferenceException
    }
    catch (Exception exp)
    { }
}

The given exception message is:

Object reference not set to an instance of an object.

Stack trace:

at System.Buffer.Memmove(Byte& dest, Byte& src, UIntPtr len) at System.Runtime.CompilerServices.DefaultInterpolatedStringHandler.AppendFormatted(String value) at framework.POCs.NullReferenceException() in /Users/jmrpineda/Projects/GitHub/PinedaTec.eu/InGameServices/arn/arn.test/framework/POCs.cs:line 63

I marked this issue as stopper due I have a lot of lines using this try/catch and short string format

If you need additional info, please ask me

thanks, regards


Original Comments

Feedback Bot on 11/17/2022, 06:08 PM:

(private comment, text removed)

Josema R. Pineda on 11/17/2022, 11:15 PM:

(private comment, text removed)

Josema R. Pineda on 11/18/2022, 00:13 AM:

(private comment, text removed)

Feedback Bot on 11/29/2022, 08:59 AM:

(private comment, text removed)

Feedback Bot on 11/29/2022, 11:48 AM:

(private comment, text removed)


Original Solutions

(no solutions)

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 15
  • Comments: 61 (31 by maintainers)

Commits related to this issue

Most upvoted comments

I have experienced that a possible workaround is to set a new breakpoint below the code-line with string interpolation, and run to it instead of stepping over it.

Hmm… not good… for all of us on MacOs 😦

Looking at this some more it’s a bit of an issue between the JIT emitted code and what the debugger believes the this pointer is for the DefaultInterpolatedStringHandler is (probably construct this relative to an instrumented method body, but still need to investigate more - but that’s likely since it’s a ref struct). We end up calling memmove on a null pointer for the Span<char> in the DefaultInterpolatedStringHandler. A workaround for this while we figure if a fix that’s easy to backport is to set DOTNET_EnableAVX=0 in the environment setting before launching the app to be debugged.

MS 8.0.0 (Plan: November 23)? This implies buggy debugging until November?

Our team’s workflow is to first address the active issue in the active development branch which is currently “main”. All fixes/features in main will ship with 8.0. Once we have a fix, for issues that are impacting customers in existing .NET Core releases, our workflow is to take this issue to the .NET servicing team to backport to the requested servicing release(s). So the milestone date does not necessarily correspond to the first release to customers as we have the ability to backport the changes to servicing (once the fix is available). This issue is on our team’s backlog and we plan on investigating this up after our existing/assigned work is completed.

@tommcdon Correct. When debugging with both rider and vscode, it works as expected if I add DOTNET_EnableAVX=0 to my environment variables.

Still seeing this issue on macOS with the 7.0.9 that was released today.

Just encountered this issue too. Is there any more information on a fix date? This is a huge blocker.

MacBook Pro 2020 macOS Monterey 12.5 2 GHz Quad-Core Intel Core i5 .NET 7

I have Mac OS 12.6.1 Intel Core i7 x64 dotnet 7.0.100 Using VS Code or Visual Studio for Mac 17.4.1 (build 28)

My coworker in on M1 Ventura and he is not having any problems.

I got the same error too. IDE: Jetbrains Rider macOS : Sonoma 14.1.1 (22A400)
with SDK 8.0, Runtime 8.0

This issue doesn’t repro if COMPlus_EnableAVX=0. That could be a work-around.

@c-ed @jchannon @rmarinho The referenced Roslyn fix addresses a performance problem related to string interpolation and string concatenation. This issue is debugger specific and is under investigation. Would you mind confirming that #78991 (comment) posted by @hoyosjs works around the issue?

Yes, I just confirmed. I used Visual Studio Mac with a web project. First this was the exception. Screenshot 2023-07-19 at 18 04 27

So I set the env variable on the configuration. Screenshot 2023-07-19 at 18 05 21

And voila it works: Screenshot 2023-07-19 at 18 06 16

The way around it for me is to call ToString() which causes different IL to be created and doesn’t use DefaultInterpolatedStringHandler

Example:

var requestUri = $"/public-api/notifications/v1.0/{existingWebhookRegistration.WebhookId.ToString()}";

same issue here:

macbook pro 2019 2,4 GHz 8-Core Intel Core i9 64 GB 2667 MHz DDR4 .NET 7

using a regular string interpolation fails during debugging

] 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.AppendFormatted(String value)

I had to replace it with a string.format : $"{streetName} {request?.Number} {request?.Box}, {city?.Zip} {city?.Name}"

 var address = string.Format("{0} {1} {2}, {3} {4}", streetName, request?.Number, request?.Box,
                    city?.Zip, city?.Name);

euuueeueuuehehehe WHAT !!! i strongly sugges fix it ASAP, we can’t tell our management to wait for more than half a year t use normal code