0

As the title says, I'm having an issue with ifstream. I was trying to read a text file and that would work fine when debugging but not in the release build. I was able to narrow it down to the example below.

The following code segfaults when it reaches the declaration of file in foo() if optimizations are turned on.

#include <iostream>
#include <fstream>

void foo (){
    std::cout << "I'm here 3" << std::endl;

    std::ifstream file;

    std::cout << "I'm here 4" << std::endl;
}

int main()
{
    std::cout << "I'm here 1" << std::endl;

    std::ifstream file;

    std::cout << "I'm here 2" << std::endl;

    foo();

    return 0;
}

Output:

I'm here 1
I'm here 2
I'm here 3

I'm compiling and linking with the following commands:

g++ -Wall -std=c++17 -O1 main.cpp -o main.exe 

If I change the -O1 to O0 it doesn't segfault and runs to the end. I'm not really sure what the issue is.

My system is Windows 10 and I'm running g++.exe (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0. Is this a compiler bug or am I just doing something stupid?

Archie
  • 13
  • 3
  • What happens if you add `--static` to the flags? – HolyBlackCat Nov 03 '21 at 19:32
  • GCC 8 is way way out of date. Can you try with a recent version? – Ben Voigt Nov 03 '21 at 19:35
  • @HolyBlackCat that fixed it. Whats it doing? – Archie Nov 03 '21 at 19:36
  • @BenVoigt This seems to be the lastest I can get (https://www.mingw-w64.org/downloads/#mingw-builds). If you have a better alternative for windows I'm happy to give it ago. – Archie Nov 03 '21 at 19:40
  • 3
    [Instructions to get an up-to-date tool chain](https://stackoverflow.com/a/30071634/4581301) along with a package manager so you can easily keep it up to date AND have an impressive ecosystem of pre-built libraries so you don't have to screw around playing mix-n-match with library binaries or roll your own library builds. – user4581301 Nov 03 '21 at 19:44
  • I second user4581301, MSYS2 is great. But by itself it's unlikely to solve your problem, see my answer. – HolyBlackCat Nov 03 '21 at 19:47

1 Answers1

5

Since --static fixes it, this means your program picks up incorrect DLL versions.

Open your compiler's bin directory, and make a list of all DLLs in there.

Then:

  • Search for those DLLs in C:\Windows (recursively). Delete any matching DLLs, they shouldn't be there.

  • Make sure your compiler is the first thing in the PATH, or at least that any directories before it don't contain those DLLs (or better, all those directories except the compiler one).

    This includes not having two different MinGW versions in the PATH (or you can just uninstall all but one).

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 1
    While I agree that none of the DLLs should be in both the OS and in the compiler-local directory, assuming that all of these should be compiler-local carries a HUGE risk of totally breaking the OS install. – Ben Voigt Nov 03 '21 at 19:47
  • @BenVoigt I agree, but not sure how to make it safer. Any ideas? – HolyBlackCat Nov 03 '21 at 19:54
  • I couldn't find any matching DLLs in `C:\Windows` and moving it to the top of the path didn't seem to have an effect. – Archie Nov 03 '21 at 19:55
  • @Archie Then check the remaining dirs in PATH for those dlls. – HolyBlackCat Nov 03 '21 at 19:58
  • @HolyBlackCat There are quite a few things in the path have those DLL. I've got a Ruby install that has its own MinGW. I've installed MSYS2 and that has fixed it. – Archie Nov 03 '21 at 20:00
  • @Archie You can try running [`ntldd -R your_program.exe`](https://github.com/LRN/ntldd) to see which dlls it finds. *"MSYS2 ... fixed it"* Didn't expect that, but all is well then. :) – HolyBlackCat Nov 03 '21 at 20:01
  • This is why I hate it when a tool insists on placing stuff in the path. Everything works, then something's installed or updated and winds up being found in the path before whatever whatever you're currently running and ka-blooey. Magical Mystery Bug. – user4581301 Nov 03 '21 at 20:35
  • 1
    @HolyBlackCat: For example, checking if the DLL publisher is Microsoft before deleting... – Ben Voigt Nov 04 '21 at 17:50