【问题标题】:Surprising CLR / JIT? behaviour - deferred initialization of a local variable令人惊讶的 CLR / JIT?行为 - 局部变量的延迟初始化
【发布时间】:2012-03-25 20:01:57
【问题描述】:

我刚刚在Debug 模式(VS 2008 ExpressAny Cpu)下运行应用程序时遇到了一些非常奇怪的事情。如果有人启发我了解这里发生了什么,我将不胜感激?

// PredefinedSizeGroupMappings is null here
Dictionary<string, int> groupIDs = PredefinedSizeGroupMappings ?? new Dictionary<string, int>();

// so groupIDs is now initialized as an empty Dictionary<string, int>, as expected

// now: PredefinedSizesMappings is null here - therefore I expect sizeIds
// to be initialized as an empty dictionary:
Dictionary<string, string> sizeIds = PredefinedSizesMappings ?? new Dictionary<string, string>(); 

// but at this point sizeIds is still null! :O That's what debugger shows.
var groupsReport = new AutomappingReportArgs();

// only once we get here - it's suddenly not... The debugger shows: "Count = 0"
var sizesReport = new AutomappingReportArgs();

AutomappingReportArgs 类与 sizeIds 变量没有任何联系,尽管它的构造函数确实分配了许多字典:

public AutomappingReportArgs()
{
    ChangedNames = new Dictionary<string, KeyValuePair<string, string>>();
    CreatedAfterRename = new Dictionary<string, string>();            
    Existing = new Dictionary<string, string>();
    Created = new Dictionary<string, string>();
    Failed = new Dictionary<string, string>();
}

我猜它一定是某种编译器或 CLR 优化,但我想更详细地了解它的机制。这种“延迟初始化”的原因是什么?

为什么它不一致,为什么它对Dictionary&lt;string, int&gt; 立即起作用,但对Dictionary&lt;string, string&gt; 不起作用?是不是因为编译器前面看不到任何Dictionary&lt;string, int&gt;初始化,所以不能放在一边待会儿?

【问题讨论】:

  • 您是否清理过构建并重试?
  • 我重复了几次。在什么意义上清理它?
  • 像右键单击解决方案然后单击“清除解决方案”一样清洁。
  • VS 2008 Express 中没有这样的东西
  • 那么应该有一个重建。

标签: c# debugging .net-3.5 clr jit


【解决方案1】:

这是调试优化代码时的标准行为。这里不太可能是这种情况。可能是调试器中的错误。 VS2008 有一个重要的 SP1 后修补程序修复了许多调试器问题。

您可以在this answer 中找到该修补程序的链接。不太确定该修补程序对 Express Edition 的适用程度,您应该没问题,但我不能保证。

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 2011-11-29
    • 2019-10-05
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多