Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private static readonly unused intializers removed from optimized build. #13526

Closed
malylemire1 opened this issue Oct 3, 2019 · 2 comments
Closed
Labels
question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@malylemire1
Copy link

Here is an example of the behavior I get :

public class MyClass
{
private static readonly MyClass _instance = new MyClass();

    private MyClass()
    {
        Console.WriteLine("Never called in release build.");
    }

    ~MyClass()
    {
        Console.WriteLine("Never called in release build.");
    }

}

public class MyClass
{
private static readonly MyClass _instance;

static MyClass()
    {
        _instance = new MyClass();
    }

    private MyClass()
    {
        Console.WriteLine("Called in release build.");
    }

    ~MyClass()
    {
        Console.WriteLine("Called in release build.");
    }

}

@malylemire1
Copy link
Author

Sorry about this. I'm using .net full framework. Don't know if you can reproduce the behavior in .net core.

@jkotas
Copy link
Member

jkotas commented Oct 4, 2019

The behavior you are seeing is expected. Your first example has "before field init" constructor that is not guaranteed to run. The runtime can choose whether to run it or not, and the behavior can differ between debug and release builds.

https://github.com/dotnet/coreclr/issues/1193 and https://github.com/dotnet/coreclr/issues/25580 have more discussion about this problem.

@jkotas jkotas closed this as completed Oct 4, 2019
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

2 participants