Segfault with asio standalone when classes in separate files

后端 未结 3 454
死守一世寂寞
死守一世寂寞 2020-12-31 04:39

The below is as minimal of an example as I can get. It does need to be in separate files as that seems to be what causes the segmentation fault error.

I\'m using Min

相关标签:
3条回答
  • 2020-12-31 05:18

    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.

    0 讨论(0)
  • 2020-12-31 05:31

    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

    0 讨论(0)
  • 2020-12-31 05:40

    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.

    0 讨论(0)
提交回复
热议问题