Segmentation fault when compiling with mingw g++ 7.2, declaring an ifstream and using extern "C"

1

I'm was writing some interfacing code to call c++ routines from fortran which is why I dumped the methods into an extern "C" block. When compiling with g++.exe (x86_64-win32-seh-rev1, Built by MinGW-W64 project) 7.2.0 under Windows 10 I was getting segmentation faults when declaring an ifstream object. I broke it down to the following code snippet:

tester.cpp

#include <fstream>

void x(){
    std::ifstream infile;
}

extern "C"
{
    void whatever() {
    }
}

int main(){
    x();
    return 0;
}

I'm building with following commmand: g++ tester.cpp -o tester.out -O3. The program runs without faults when building with -O0 but crashes for -O1,-O2 and -O3. Compiling using msvc under Windows and g++ 8.30 under Linux produces no segmentation fault. Both the extern "C" block and the call to x() are required for the program to crash.

Running gdb backtrace gives the following output:

    Thread 1 received signal SIGSEGV, Segmentation fault.
0x000000006fce18b2 in ?? ()
   from C:\Program Files\Git\mingw64\bin\libstdc++-6.dll
(gdb) backtrace
#0  0x000000006fce18b2 in ?? ()
   from C:\Program Files\Git\mingw64\bin\libstdc++-6.dll
#1  0x000000006fcf7b6c in ?? ()
   from C:\Program Files\Git\mingw64\bin\libstdc++-6.dll
#2  0x000000006fcf9b41 in ?? ()
   from C:\Program Files\Git\mingw64\bin\libstdc++-6.dll
#3  0x000000000040163a in std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream (this=0x61fc20, __in_chrg=<optimized out>,
    __vtt_parm=<optimized out>)
    at C:/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/fstream:482
#4  x () at tester.cpp:4
#5  0x0000000000402ede in main () at tester.cpp:14

Is this a bug with mingw or am I missing something. std::ifstream crashes in release build on Windows with exit code 0xC0000409: Unknown software exception might be related.

c++
segmentation-fault
mingw
ifstream
extern-c
asked on Stack Overflow May 4, 2020 by Peter L. • edited May 4, 2020 by Peter L.

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0