2

I'm upgrading from VS2013 to VS2015 and am getting the following warning. I thought VS2015 implemented magic statics so that the local static object should be thread-safe, so what is going on?

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(698): error C2220: warning treated as error - no 'object' file generated
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(704): note: see reference to function template instantiation '_Ty &std::_Immortalize<std::_Generic_error_category>(void)' being compiled
          with
          [
              _Ty=std::_Generic_error_category
          ]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(698): warning C4640: '_Static': construction of local static object is not thread-safe

The error is in the system_error header from the VS installation folder. The error is in this function:

template<class _Ty> inline
    _Ty& _Immortalize()
    {   // return a reference to an object that will live forever
    static _Immortalizer<_Ty> _Static;
    return (*reinterpret_cast<_Ty *>(&_Static._Storage));
    }

That's all the context there is in the error, and I can't see where system_error actually gets included.

Compiler flags are:

/Yu"stdafx.h" /GS /analyze /W3 /wd"4481" /wd"4251" /Zc:wchar_t 
/Zi /Gm- /O2 /sdl /Fd"x64\Release\\Release_vc140.pdb" /Zc:inline /fp:precise
/errorReport:prompt /WX /Zc:forScope /Gd /MT /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\MyProj.pch"

Update

Sorry, it seems fixed now. It looks like I was on the v140_xp toolset and wrong TargetPlatformVersion. This one must have slipped through the net when I thought I'd replaced them all. I'm not quite sure why getting those wrong would result in this error though. Anyway, thanks for the help so far.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204

2 Answers2

2

It looks like its caused by the bogus warning which is turned into an Error, read this connect bug:

https://connect.microsoft.com/VisualStudio/feedback/details/2539759/c4640-warning-cannot-be-disabled

The error C2220 indicates you have enables /WX switch, which tells the compiler to treat all warnings as errors.

you can turn this warning off with:

#pragma warning (disable : 4640) 

btw. unrelated, but might be usefull to You. You use _Immortalizer as a name for your class which starts with underscore followed by uppercase latter. This is prohibited by standard: read here: What are the rules about using an underscore in a C++ identifier?.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • 2
    That's not my class, that's from the standard headers which is where the error occurs. – Scott Langham Apr 18 '16 at 13:42
  • @ScottLangham Oh, I understand - If your problem is no longer reproducible then delete question – marcinj Apr 18 '16 at 13:44
  • 1
    I can reproduce it, so I would think this question might still be useful if somebody else has their project incorrectly configured and gets the same error. – Scott Langham Apr 18 '16 at 13:46
2

It's caused by changes in MSBuild's toolset v140_xp. Issue appears when:

  • project builds with VS2015 Update 2, toolset v140_xp
  • project configuration is Dynamic Library
  • project was created with ATL App Wizard, so *.vcxproj contains tag <Keyword>AtlProj</Keyword>

There is workaround: open *.vcxproj file, find tag <Keyword>AtlProj</Keyword> and replace it with <Keyword>Win32Proj</Keyword>. Nothing will be changed while compiling, but warning will disappear.

If you want to know details, navigate to the directory C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140_xp, open Toolset.props and find comment <!-- Added /Zc:threadSafeInit- for ATL dll projects and enable the C4640 warnning -->. The line below it enables warning and disables "magic static" feature from C++11.

P.S. Please note that warning isn't useless: 'magic static' uses thread-local storage which suffers from ugly WindowsXP kernel bug: access to TLS from DLL causes crash when application built without TLS support. Any DLL plugins with WinXP support affected by this bug.

Sergey Shambir
  • 1,562
  • 12
  • 12