VS2015 C ++静态初始化崩溃,可能的bug [英] VS2015 C++ static initialization crash, possible bug

查看:172
本文介绍了VS2015 C ++静态初始化崩溃,可能的bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Visual Studio 2015社区发生了一些奇怪的事情。在VS2012工作完美的代码崩溃时启动时移植到VS2015,主要被调用之前:一些静态初始化的经典症状。我有一些静态变量,但正确使用标准的首次使用构造模式,例如:

  const int& ; i()
{
static int * v = new int(1);
return * v;
}

上面的代码导致在初始化程序时从运行时断言 http://i.stack.imgur.com/nsQGS.png )。



下面的代码无缺陷地工作:

  const int& i()
{
static int * v = nullptr;
if(v == nullptr)
v = new int(1);
return * v;
}



在我看来VS2015正在做一些(即使在调试版本中),通过在程序初始化期间执行静态变量初始化代码,而不是第一次调用函数,如C ++标准所要求的。
我尝试了上面代码的几个变体:类方法,自由函数,不同的对象(std :: vector,cv :: Mat),总是具有相同的结果:静态指针必须初始化为null或程序不启动。



所以...我失去了什么东西,或者VS2015实际上搞错了?



UPDATE:



我花了一些时间尝试找到显示问题的最小设置,这是我得到的:


  1. 使用CLR空项目模板创建项目。

  2. 添加一个包含错误函数和main 。

  3. 添加main作为入口点(或者您收到链接错误)。
  4. >

x86版本可以正常工作,但x64版本没有。



为了进行比较,即使添加了/ CLR选项,具有相同代码但创建为Win32控制台应用程序的项目也不会出现问题。我可以看到vcxproj文件的差异,但没有证明错误,虽然其中一个或多个明显是原因。



有一种方法来上传zip该项目?

解决方案

好吧,@bogdan是正确的。我的项目有混合设置,既不是/ SUBSYSTEM:CONSOLE也不是/ SUBSYSTEM:WINDOWS。一旦我修复了,一切开始按预期工作。我的测试项目有同样的问题,我责怪微软没有一个清晰的CLR Windows应用程序C ++模板在VS2015(他们想推动你使用C#,这是有意义的大多数时间,但不总是)。



我发现此页面特别适用于解释必须一致的所有不同设置(它不只是/SUBSYSTEM...)./ >

我希望我可以将@ bogdan的评论标记为答案,但我看不到任何东西。



谢谢Bogdan!


I'm seeing something weird happening with Visual Studio 2015 Community. Code that worked perfectly in VS2012 crashes at startup when ported to VS2015, before main is invoked: the classic symptoms of some static initialization mess. I have a few static variables, but used properly, with the standard "Construct On First Use" pattern, e.g.:

const int& i()
{
  static int *v = new int(1);
  return *v;
}

The code above causes an assert from the runtime while initializing the program (see image http://i.stack.imgur.com/nsQGS.png). Pressing retry simply quits the program: no call stack, no information whatsoever.

The code below however works flawlessly:

const int& i()
{
  static int *v = nullptr;
  if (v == nullptr)
    v = new int(1);
  return *v;
}

It seems to me that VS2015 is doing what looks like some (illegal) optimization (even in a debug build), by executing the static variable initialization code during program initialization, and not the first time that the function is called, as the C++ standard requires. I tried several variations of the code above: class methods, free functions, different objects (std::vector, cv::Mat), always with the same result: the static pointer has to be initialized to null or the program doesn't start.

So... am I missing something or is VS2015 actually messing up badly?

UPDATE:

I spent some time trying to find the minimal setup that shows the problem, and this is what I got:

  1. create a project using the "CLR empty project" template.
  2. add a cpp file, containing just the offending function and main(). Main can be empty, it doesn't matter: the bug occurs BEFORE it is even reached.
  3. add 'main' as entry point (or you get a link error).

The x86 version works, but the x64 doesn't.

For comparison, a project with the identical code but created as "Win32 console application" doesn't have the problem, even after adding the /CLR option. I can see differences in the vcxproj files, but none justifies the error, although one or more of them clearly IS the cause.

Is there a way to upload a zip with the project?

解决方案

Well, @bogdan got it right. My project had a mix of settings that was neither /SUBSYSTEM:CONSOLE nor /SUBSYSTEM:WINDOWS. Once I fixed that, everything started to work as expected. My test projects had the same problem, I blame Microsoft for not having a clear "CLR windows app" C++ template any more in VS2015 (they want to push you to use C# for that, which makes sense most of the times, but not always).

I found this page particularly useful in explaining all the different settings that have to be consistent (it's not just /SUBSYSTEM...).

I wish I could mark @bogdan's comment as answer, but I can't see anything to do that.

Thanks Bogdan!

这篇关于VS2015 C ++静态初始化崩溃,可能的bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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