What's the difference in these ways of creating the static instance for a singleton?

前端 未结 2 1730
栀梦
栀梦 2021-02-02 04:06

I have had a bug recently that only manifested itself when the library was built as a release build rather than a debug build. The library is a .NET dll with a COM wrapper and

2条回答
  •  温柔的废话
    2021-02-02 04:42

    Try adding an (empty) static constructor, or initialize the singleton in a static constructor.

    Jon Skeet has a full discussion of singleton patterns here. I'm not sure why it failed, but at a guess it could relate to the "beforefieldinit" flag. See his 4th example, where he adds a static constructor to tweak this flag. I don't claim to be an expert on beforefieldinit, but this symptom seems to fit some of the symptoms discussed here.

提交回复
热议问题