Search code examples
multithreadingc++11stdatomic

std::atomic<std::string>: Access violation writing location 0xFEEEEFEEE in Visual Studio 2013 (VC12) - does not occur when using std::atomic<int>


The following minimal code example compiled in Debug mode for x64 and run in the Visual Studio 2013 debugger yields an

Unhandled exception at ...: Access violation writing location 0xFEEEFEEE.

When debugging I see that the access violation occurs at the "return 0;" statement.

(When run from the console without debugger, the error reads "Instruction at 0x... referenced memory at 0xddddddd... The memory could not be written.").

#include <atomic>
#include <string>

int main()
{
  std::atomic<std::string> a1("127.0.0.1:41001");
  std::string ep1_1 = a1.load();
  std::string ep1_2 = a1.load();
  return 0;
}

Solution

  • std::atomic<std::string> is not legal in standard C++, because std::string is not trivially copyable. Unfortunately there is no requirement for a compiler to refuse this code, but there is also no requirement (and no likelihood) that it will work properly either.

    See also: Does std::atomic<std::string> work appropriately?