[Solved] Runtime error with Mingw, ifstream, Qt Creator



  • Hello,

    I have a problem with ifstream and the following code:
    @
    #include <fstream>
    #include <iostream>

    class myClass{
    private:
    public:
    myClass(){};
    void doSomething(std::string path){
    std::string gl;
    std::ifstream file;

        file.open(path.c_str(),std::ios_base::in);
    
        while(std::getline(file,gl)){
            std::cout << gl << std::endl;
        }
        file.close();
    };
    

    };

    int main(){
    myClass myclass;
    myclass.doSomething("text.txt");
    std::ifstream file;
    return 0;
    }
    @

    There is no problem to compile and run this code with Qt Creator and Mingw from Qt Creator. A console opens and presents me the content of my text.txt. But when I copy text.txt into the Release folder and start the exe file, I get a message box with an error but without helping message. There is only something like "The application has stopped working" like:
    !http://www.geekersmagazine.com/wp-content/uploads/2011/01/windows-explorer-has-stopped-working.gif(The application has stopped working)!

    So I downloaded mingw and startet mingw32-make on Makefile.Release. Everything seemed fine. I configured Qt Creator to use my new mingw. But then I get the same error, when I run my code out of Qt Creator. I hope, someone has an idea what to do.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you by any chance getting error 0xc000135 ?



  • No, I don't get any error code.

    This works fine:
    @
    #include <fstream>
    #include <iostream>

    class myClass{
    private:
    public:
    myClass(){};
    void doSomething(std::string path){
    //std::string gl;
    };
    };

    int main(){
    myClass myclass;
    myclass.doSomething("text.txt");
    std::ifstream file;
    return 0;
    }

    @

    and this works fine too:

    @
    #include <fstream>
    #include <iostream>

    class myClass{
    private:
    public:
    myClass(){};
    void doSomething(std::string path){
    std::string gl;
    };
    };

    int main(){
    myClass myclass;
    myclass.doSomething("text.txt");
    //std::ifstream file;
    return 0;
    }

    @

    but this doesn't work:

    @
    #include <fstream>
    #include <iostream>

    class myClass{
    private:
    public:
    myClass(){};
    void doSomething(std::string path){
    std::string gl;
    };
    };

    int main(){
    myClass myclass;
    myclass.doSomething("text.txt");
    std::ifstream file;
    return 0;
    }
    @


  • Lifetime Qt Champion

    What does your pro file look like ?



  • That is the pro file:
    @
    #-------------------------------------------------

    Project created by QtCreator 2014-08-27T16:59:28

    #-------------------------------------------------

    QT += core

    QT -= gui

    TARGET = ifstream
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp
    @

    Remember, that the Makefile is ok, since I can use mingw32-make with extern g++ from extern mingw to compile the project.

    gcc version: 4.8.1
    GNU Make 3.82.90



  • Can you reproduce this problem? I work on two different machines, a laptop and a desktop pc. Both with Win 7 Pro 64 bit. And the error occurs on both systems.

    After inserting another std::string, the following code works fine again.

    @
    #include <iostream>
    #include <fstream>

    class myClass{
    private:
    public:
    myClass(){};
    void doSomething(std::string path){
    std::string gl;
    };
    };

    int main(){
    myClass myclass;
    myclass.doSomething("text.txt");
    std::string path;
    std::ifstream file;
    return 0;
    }
    @

    I don't get it. Absolut senseless.


  • Lifetime Qt Champion

    There is no problem. You are linking to the QtCore library, which doesn't really make sense since you are not using Qt at all. So when starting outside Qt Creator your program can't find QtCore.dll. So you can either follow the Windows Deployment Guide or drop the Qt dependency



  • I changed the pro file to
    @
    #-------------------------------------------------

    Project created by QtCreator 2014-08-27T16:59:28

    #-------------------------------------------------

    #QT += core

    QT -= gui

    TARGET = ifstream
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp
    @
    Nothing changed. When I start the application from outside Qt, it crashes.

    Since I have other, bigger projects with Qt but without std::ifstream and no error occurs, I am sure, that my system finds the dll's. They are added to the system environment. I even tried Dependency Walker. No problems with dll's.

    Now I tried to apply mingw32-make from Qt folder on the makefile. And everything works fine even with linked Qt libs.

    The problem is Qt Creator. When I compile with Qt Creator, I can't run the application from outside Qt Creator. When I configure Qt Creator to use my Mingw, I can run the application from outside Qt Creator but it crashes, when I run the code from inside Qt Creator.

    The most mysterious fact is, that without std::ifstream everything works fine. I used Qt Creator for many projects without problems. But now I need a file stream and this problem occurs the first time.

    I tried now an older project with Qt gui, message box and other Qt relevant features. Everything is ok. It's really just the ifstream



  • I think, that I unterstand the problem. I have two different, incompatible mingw on my system disturbing each other. When I compile the code with Qt Creator, g++ from Qt Creator is used. When I run the code from Qt Creator, the libstdc++-6.dll from Qt Creator is linked. When I run the application from outside Qt Creator, the wrong libstdc++-6.dll is linked.

    I read on the internet, that the header file for fstream from Qt Creator's mingw are different to other mingw's header files. So the library are different too.

    I copied the libstdc++-6.dll from Qt Creator into my project folder and everything works. The problem is solved for me. Thanks.


  • Lifetime Qt Champion

    Having multiple MinGW version on your computer is generally the shortest way to problems. The simple way is to use the version your got with the MinGW package.

    It's not Qt Creator's MinGW, it's a MinGW version known to work well to build Qt. However, and AFAIK, there's no special modification done.

    Anyway, glad you got it working.

    Happy coding !



  • Just for your interest, commenting the line #CONFIG += core like you did will not drop the core module. qmake will include the core and gui modules by default, even if your .pro file is empty. Read more here: http://qt-project.org/doc/qt-5/qmake-project-files.html#declaring-qt-libraries

    So it's better for you to use CONFIG -= core gui to remove these dependencies.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.