runtime icon indicating copy to clipboard operation
runtime copied to clipboard

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

Open vsfeedback opened this issue 4 years ago • 65 comments

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)

vsfeedback avatar Nov 29 '22 20:11 vsfeedback

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Nov 29 '22 20:11 ghost

Additional info:

Visual Studio for Mac preview 17.5 Preview (17.5 build 437)

macOS: Ventura 13.0.1 (22A400)

Project info:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<IsPackable>false</IsPackable>
		<ReleaseVersion>0.0.5.4</ReleaseVersion>
	</PropertyGroup>
	<ItemGroup>

If you downgrade the target framework to net 6.0, the test works fine.

jmrpineda avatar Nov 29 '22 23:11 jmrpineda

Is this on an x64 or ARM64 Mac?

MichalPetryka avatar Nov 29 '22 23:11 MichalPetryka

Is this on an x64 or ARM64 Mac?

x64, Intel Core i9

jmrpineda avatar Nov 30 '22 04:11 jmrpineda

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

Issue Details

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)

Author: vsfeedback
Assignees: -
Labels:

area-System.Runtime, untriaged

Milestone: -

ghost avatar Nov 30 '22 08:11 ghost

Similar issue here.

Mac OS 13.0.1 (22A400) dotnet 7.0.100 Using VS Code (but probably it doesn't matter).

csproj (partial): <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>disable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <LangVersion>latest</LangVersion> </PropertyGroup>

code snippet (all with value): var uri = $"https://api.openweathermap.org/data/2.5/weather?appid={apiKey}&lat={latitude}&lon={longitude}";

exception stack trace (partial):

at System.Buffer.Memmove(Byte& dest, Byte& src, UIntPtr len)
at System.Runtime.CompilerServices.DefaultInterpolatedStringHandler.AppendStringDirect(String value)
...

mkrivacek avatar Dec 08 '22 13:12 mkrivacek

@mkrivacek Can you provide minimal repro code that compiles?

danmoseley avatar Dec 08 '22 13:12 danmoseley

I have the same issue. It has something to do with Threading on macOS?

@danmoseley Here's a repo console app:

using System.Collections.Concurrent;

BlockingCollection<int> Queue = new();

void ProcessQueue()
{
    foreach (var item in Queue.GetConsumingEnumerable())
    {
        var number = 100;

        // Works fine
        Console.WriteLine($"{DateTime.Now.ToString()}");
        Console.WriteLine($"{number.ToString()}");

        // Fails here with NullReferenceException - same as the exception in first note.
        Console.WriteLine($"{DateTime.Now}");
        Console.WriteLine($"{number}");
    }
}

var t = new Thread(ProcessQueue);
t.Start();

Queue.Add(0);

t.Join();

Code-DJ avatar Dec 08 '22 17:12 Code-DJ

@stephentoub

danmoseley avatar Dec 08 '22 17:12 danmoseley

Does this happen in both debug and release builds?

In and out of the debugger?

What happens if you disable tiered compilation? e.g.

export DOTNET_TieredCompilation=0

?

stephentoub avatar Dec 08 '22 19:12 stephentoub

For me it only happens when debugging.

Works fine with both debug and release builds:

dotnet build
cd bin/Debug/net7.0
dotnet Test.dll

dotnet build -c Release
cd bin/Release/net7.0
dotnet Test.dll

Not sure if relevant for debugging, added the following to csproj - still get the same exception:

  <PropertyGroup>
    <TieredCompilation>false</TieredCompilation>
  </PropertyGroup>

Code-DJ avatar Dec 08 '22 20:12 Code-DJ

@jmrpineda @mkrivacek does this only happen for you both during debugging as well? If so I think we would send this to the debugging area.

danmoseley avatar Dec 08 '22 22:12 danmoseley

@jmrpineda @mkrivacek does this only happen for you both during debugging as well? If so I think we would send this to the debugging area.

I just do a remote connection, Only when you are debugging the issue appears

jmrpineda avatar Dec 08 '22 22:12 jmrpineda

@stephentoub should we move to diagnostics team?

danmoseley avatar Dec 09 '22 03:12 danmoseley

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

Issue Details

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)

Author: vsfeedback
Assignees: -
Labels:

area-Diagnostics-coreclr, untriaged

Milestone: -

ghost avatar Dec 09 '22 03:12 ghost

Similar issue here.

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

lmilskiddev avatar Dec 09 '22 14:12 lmilskiddev

@lmilskiddev, also only when debugging?

stephentoub avatar Dec 09 '22 14:12 stephentoub

Yes and here are some other observations. This line failed AppLog.LogInfo($"cjevent Host {domain} '{cjEventData}'"); Change it to this and it stopped failing AppLog.LogInfo($"cjevent Host {domain} {cjEventData}");

I have also seen where the behavior changes based on if the debugger is at a break and you step through code. Some (but not all) of my $" lines only fail when stepping through code.

@lmilskiddev, also only when debugging?

lmilskiddev avatar Dec 09 '22 15:12 lmilskiddev

I've been trying to repro this against 7.0.1 on a Ventura m1. Neither of the programs seem to repro this for me.

hoyosjs avatar Dec 14 '22 01:12 hoyosjs

hmm -- I assume you're using release bits, @hoyosjs ?

I'm guessing you have 13.0.1 as well, as some folks above do. edit: n/m you said Ventura.

Does it only repro on x64? @lmilskiddev @mkrivacek @Code-DJ do you have an M1/M2 (Arm CPU) or an Intel CPU?

danmoseley avatar Dec 14 '22 02:12 danmoseley