I\'m very confused and I think my debugger is lying to me. I have the following loop in my code:
MyClass::UploadFile(CString strFile)
{
...
static DWORD
The problem may be related to multithreading.
Since the code is part of a member function, and you're calling this function from multiple threads, the static
variables are not thread-safe if using a compiler that does not conform to C++ 11 standards. Thus you may get data races when initializing those two static variables.
For a C++ 11 standard conforming compiler, static variables are now guaranteed to be initialized by the first thread, while subsequent threads wait until the static is initialized.
For Visual Studio 2010
and below, static local variables are not guaranteed to be thread safe, since these compilers conform to the C++ 03 and C++ 98 standard.
For Visual Studio 2013
, I am not sure of the level of C++ 11 support in terms of static local initialization. Therefore, for Visual Studio 2013, you may have to use proper synchronization to ensure that static local variables are initialized correctly.
For Visual Studio 2015
, this item has been addressed and proper static local initialization is fully implemented, so the code you currently have should work correctly for VS 2015 and above.
Edit: For Visual Studio 2013
, static local thread-safe initialization is not implemented ("Magic Statics"), as described here.
Therefore, we can cautiously verify that the reason for the original problem is the static-local initialization issue and threading. So the solution (if you want to stick with VS 2013) is to use proper synchronization, or redesign your application so that static variables are no longer needed.