0

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;
}
Alexander Malakhov
  • 3,383
  • 2
  • 33
  • 58
alex
  • 2,252
  • 4
  • 23
  • 34

1 Answers1

6

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?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436