静态成员变量未在 Release 中初始化 - 编译器/clr 错误? [英] Static member variable not being initialized in Release - Compiler/clr bug?

查看:33
本文介绍了静态成员变量未在 Release 中初始化 - 编译器/clr 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预期产出&在 VS2010、.NET 4.0 下我进入调试模式和发布模式的输出:

Expected output & output I get in debug mode, and release mode under VS2010, .NET 4.0:

bar construct
main

发布模式下的输出不是在 VS2010 调试器和 WinDbg 下:

Output in release mode not under the VS2010 debugger, and under WinDbg:

main

程序在 VS2005、.NET 2.0 上不会出现这种行为

Program does not exhibit this behavior on VS2005, .NET 2.0

using System;

namespace static_init
{
    public class bar
    {
        public bar()
        {
            Console.WriteLine("bar construct");
        }
    }

    class Program
    {
        public static bar blah = new bar();

        static void Main(string[] args)
        {
            Console.WriteLine("main");
            Console.ReadLine();
        }
    }
}

可能相关:静态构造函数可以在非静态构造函数.这是编译器错误吗?

更新

在我的实际代码构造函数中 bar() 使用 C++(非托管)初始化一些互操作代码.它需要在此库中的任何其他内容之前发生 - 有没有办法确保不放入接触所有静态的 init() 函数(具有未外部引用的副作用)在图书馆?

In my actual code constructor bar() initializes some interop code with C++ (unmanaged). It needs to happen before anything else in this library - is there any way to ensure that without putting in an init() function that touches all of the statics (with side effects that aren't externally referenced) in the library?

未来搜索者注意:我使用 SWIG,这是他们在包装器生成代码中做出的假设.SWIGStringHelper 是当前的罪犯,但可能还有更多.

Note for future searchers: I'm using SWIG, and this is an assumption that they made in their wrapper generation code. SWIGStringHelper is the current offender, there may be more though.

结论

更新到 SWIG 2.0 版,它根据较新版本的 .NET 的需要放入静态构造函数.

Update to version 2.0 of SWIG, it puts in the static constructor as needed by newer version of .NET.

推荐答案

它可能因为你不使用它而得到优化.

It's probably getting optimized out because you don't use it.

这也不是编译器错误,而是在语言规范中.

It also isn't a compiler bug, it's in the language spec.

17.4.5.1 静态字段初始化

静态字段变量初始化器类声明对应于分配的顺序是按文本顺序执行它们出现在类声明中.如果静态构造函数(第 17.11 节)存在于类中,执行发生静态字段初始值设定项紧接在执行之前静态构造函数.否则,执行静态字段初始值设定项在依赖于实现的时间在第一次使用静态之前该类的字段

The static field variable initializers of a class declaration correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (§17.11) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class

由于您从不使用 Program 类的静态字段,因此不能保证静态初始化程序会运行(尽管它可以...上面的依赖于实现的时间")

Since you never use a static field of the Program class, the static initializer isn't guaranteed to run (though it could...the 'implementation-dependent time' above)

更新
你可以通过让 Program 有一个静态构造函数来完成你想要的.

Update
You can accomplish what you want by making Program have a static constructor.

静态程序 (){}或者可能通过访问另一个(可能是虚拟的)静态变量

static Program (){} or possibly by accessing another (possibly dummy) static variable

这篇关于静态成员变量未在 Release 中初始化 - 编译器/clr 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆