Segfault with asio standalone when classes in separate files

丶灬走出姿态 提交于 2019-11-30 08:38:02

I think the segmentation fault occurs because of mismatching asio type definitions between Server.hpp and Client.hpp. In your case, this could only happen if boost changes typedefs depending on defines set by <string>, <memory> or <vector>.

What I suggest to try is either:

  1. Include the same headers in both Server/Client.hpp and main.cpp before including asio.hpp:

    #include <string>
    #include <memory>
    #include <vector>
    
    #define ASIO_STANDALONE
    #include <asio.hpp>
    
  2. Or simply include asio.hpp before any other include in your header files and remove the include from main.cpp.

From my POV, it is a gcc bug. If you use clang++ instead of g++.exe, the crash dissappears even without fiddling with #include's ordering. Passing all source files to g++ in single call also makes crash disappear. This leads me to think that there is something wrong with gcc's COFF object files emission, since clang uses linker from the same mingw distribution.

So, either use clang or apply workaround from @Wouter Huysentruit's answer.

Note that latest clang release (3.7.0) has another bug, not related with your problem, that makes linking fail. I had to use nightly snapshot, which is quite stable anyway.

I had similar problem. My app crashed on win_lock::lock(), but win_lock::win_lock() constructor was never called!

When added -D_POSIX_THREADS to compiler flags it simply started to work.

I use mingw-w64-i686-7.1.0-posix-dwarf-rt_v5-rev0 on win7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!