Jan Rüegg Jan Rüegg - 1 year ago 112
C++ Question

std::shared_ptr crash with MSVC on different computer?

Compiling and running the following simple program works fine on my developer machine (Visual Studio 2015, 64-bit).

Running the same code on a different machine crashes with the windows error dialog, even though the x64-redistributables are installed (msvcp140.dll):

#include <memory>
#include <iostream>

int main(int argc, char **argv) {
std::shared_ptr<int> test; // comment out to run on both machines

std::cout << "Done: " << std::endl;
}


Removing the line with the shared pointer makes it work.

Any idea what the problem could be here, or how to debug?

Answer Source

I found the problem, in case this is useful for anyone:

In the windows event viewer, I discovered the reason for the crash: The exception was 0xc000001d or illegal instruction. Appareantly my code was compiled using the /arch:AVX compile flag, resulting in the following code for the snippet above:

int main(int argc, char **argv) {
00007FF749A816B0  sub         rsp,48h  
00007FF749A816B4  mov         qword ptr [rsp+20h],0FFFFFFFFFFFFFFFEh  
00007FF749A816BD  vpxor       xmm0,xmm0,xmm0  
00007FF749A816C1  vmovdqu     xmmword ptr [test],xmm0  
  std::shared_ptr<int> test; // comment out to run on both machines

  std::cout << "Done: " << std::endl;
00007FF749A816C7  lea         rdx,[string "Done: " (07FF749A86C70h)]  
00007FF749A816CE  mov         rcx,qword ptr [__imp_std::cout (07FF749A8A0D8h)]  
00007FF749A816D5  call        std::operator<<<std::char_traits<char> > (07FF749A8107Dh)  
  ...

As you can see, because of the /arch:AVX the vpxor and vmovdqu instructions were generated, which do not run on processors that are too old (like the one I tested with).

The solution is to have separate versions of the executable (or separate paths through the program) with and without AVX instructions to support older processors.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download