C++ cli VS2015 C+中的NullReferenceException+/CLI发布版本

C++ cli VS2015 C+中的NullReferenceException+/CLI发布版本,c++-cli,visual-studio-2015,cil,C++ Cli,Visual Studio 2015,Cil,我在发布版本中得到“System.NullReferenceException:对象引用未设置为对象的实例”。我创建了一个示例应用程序,它模仿我的生产代码中的内容 void Abc::LogService::Log(String^ message) { try { int ret = DoProcessing(message); Exception^ ex; if (ret == 0) {

我在发布版本中得到“System.NullReferenceException:对象引用未设置为对象的实例”。我创建了一个示例应用程序,它模仿我的生产代码中的内容

void Abc::LogService::Log(String^ message)
{
    try
    {    
        int ret = DoProcessing(message);
        Exception^ ex;
        if (ret == 0)
        {
            ex = gcnew ArgumentException("Processing done.");
        }
        else
        {
            ex = gcnew ArgumentNullException("message", "Null args");
        }
        throw ex;
    }
    finally
    {
        //do someother thing.
    }
}
使用上述代码,它将异常行报告为: LogService.cpp中Abc.LogService.Log(字符串消息)处的
:第19行
,对应于
抛出ex语句

此函数的发布版本中的MSIL如下所示:

.method public hidebysig instance void  Log(string message) cil managed
{
  // Code size       46 (0x2e)
  .maxstack  4
  .locals ([0] class [mscorlib]System.Exception V_0,
           [1] class [mscorlib]System.Exception ex)
  .try
  {
    IL_0000:  ldarg.0
    IL_0001:  ldarg.1
    IL_0002:  call       instance int32 Abc.LogService::DoProcessing(string)
    IL_0007:  ldnull
    IL_0008:  stloc.1
    IL_0009:  brtrue.s   IL_0018
    IL_000b:  ldstr      "Processing done."
    IL_0010:  newobj     instance void [mscorlib]System.ArgumentException::.ctor(string)
    IL_0015:  stloc.0
    IL_0016:  br.s       IL_0028
    IL_0018:  ldstr      "message"
    IL_001d:  ldstr      "Null args"
    IL_0022:  newobj     instance void [mscorlib]System.ArgumentNullException::.ctor(string,
                                                                                     string)
    IL_0027:  stloc.0
    IL_0028:  ldloc.1
    IL_0029:  throw
    IL_002a:  leave.s    IL_002d
  }  // end .try
  finally
  {
    IL_002c:  endfinally
  }  // end handler
  IL_002d:  ret
} // end of method LogService::Log
从MSIL代码可以看出,在语句IL_0028中,它加载一个空值,并在后续语句中调用throw。 奇怪的是,只有当我使用try finally块时才会发生这种情况。 上述代码的调试构建工作正常


这听起来像是VS2015 v140工具包中的一个bug吗?

是的,这是一个优化器bug。非常不寻常,这是我在C++/CLI中看到的第一个,在C++/CLI语言中,抖动应该起到很重要的作用。通过在try块中声明
ex
变量,使其在初始化保证下阻塞,它似乎被触发。看起来像是流分析错误

除了使用/Od编译之外,一种解决方法是将变量移出try块

void Log(String^ message) {
    Exception^ ex;
    try {
       // etc...
}
同时产生更好的MSIL,完全消除了变量:

.method public hidebysig instance void  Log(string message) cil managed
{
  // Code size       41 (0x29)
  .maxstack  4
  .try
  {
    IL_0000:  ldarg.0
    IL_0001:  ldarg.1
    IL_0002:  call       instance int32 Test::DoProcessing(string)
    IL_0007:  brtrue.s   IL_0015
    IL_0009:  ldstr      "Processing done."
    IL_000e:  newobj     instance void [mscorlib]System.ArgumentException::.ctor(string)
    IL_0013:  br.s       IL_0024
    IL_0015:  ldstr      "message"
    IL_001a:  ldstr      "Null args"
    IL_001f:  newobj     instance void [mscorlib]System.ArgumentNullException::.ctor(string,
                                                                                     string)
    IL_0024:  throw
    IL_0025:  leave.s    IL_0028
  }  // end .try
  finally
  {
    IL_0027:  endfinally
  }  // end handler
  IL_0028:  ret
} // end of method Test::Log
优化器错误很糟糕,您可以在connect.microsoft.com上报告