microsoft / STL

MSVC's implementation of the C++ Standard Library.
Other
9.88k stars 1.45k forks source link

VCRuntime incompatibility with older version in mutex code #4730

Closed roozbehid-ic closed 1 week ago

roozbehid-ic commented 1 week ago

Describe the bug

VS 17.10 C++ runtime broke following code. Code is compiled with VS 17.10 but it doesnt work anymore with previously deployed VC runtime.

Command-line test case

C:\Temp>type ConsoleApplication5.cpp
#include <iostream>
#include <mutex>
std::mutex mtx;

void testmethod() {
        std::scoped_lock lock{ mtx };
        std::cout << "lock worked!\n";
}
int main()
{
    std::cout << "Start!\n";
    testmethod();
    std::cout << "End!\n";
}

Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33811 for x64
  Copyright (C) Microsoft Corporation.  All rights reserved.
  cl /c /Zi /W3 /WX- /diagnostics:column /sdl /O2 /Oi /GL /D NDEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /std:c++17 /permissive- /Fo"x64\Release\\" /Fd"x64\Release\vc143.pdb" /external:W3 /Gd /TP /FC /errorReport:prompt ConsoleApplication5.cpp

C:\Temp>.\ConsoleApplication5.exe
Start!

C:\Temp>

Expected behavior

Lock does not crash. Crashing computer had following version of VCRuntime msvcp140.dll 14.32.31326.0 vcruntime140.dll 14.32.31326.0

working computer has msvcp140.dll 14.40.33810.0 vcruntime140.dll 14.40.33810.0


C:\Temp>.\ConsoleApplication5.exe
Start!
lock worked!
End!

C:\Temp>

``
cpplearner commented 1 week ago

This is by design. See https://github.com/microsoft/STL/wiki/Changelog#vs-2022-1710

  • Fixed mutex's constructor to be constexpr. #3824 #4000 #4339
    • Note: Programs that aren't following the documented restrictions on binary compatibility may encounter null dereferences in mutex machinery. You must follow this rule:

      When you mix binaries built by different supported versions of the toolset, the Redistributable version must be at least as new as the latest toolset used by any app component.

    • You can define _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR as an escape hatch.
roozbehid-ic commented 1 week ago

Thanks for the info. Closing the issue