简体   繁体   中英

VS2015 C++ static initialization crash, possible bug

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, eg:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM