Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using boost::filesystem::path causes program to crash
QtWS25 Last Chance

Using boost::filesystem::path causes program to crash

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 7.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Galvanic_Cell
    wrote on 4 Jan 2016, 10:05 last edited by
    #1

    Trying to use boost with Qt and having a problem with filesystem::path, I'm using QFileDialog::getExistingDirectory to return the directory I want to use and store it in a QString, then convert the QString with .toStdString() and store it in a string and then use that string with boost::filesystem::path.

    The problem is that the program crashes when boost::filesystem::path p(file.c_str()); executes, the program crashes. If I comment out that line, everything is fine. Not sure whats going on.

    #include <QDebug>
    #include <QFileDialog>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <boost/filesystem.hpp>
    #include <boost/filesystem/path.hpp>
    #include <iostream>
    #include <cstring>
    
    void MainWindow::onBrowse()
    {
        //open file select and set selected directory equal to browseDir
        browseDir = QFileDialog::getExistingDirectory(this, "Choose Working Directory", "/home", QFileDialog::ShowDirsOnly);
        ui->browseEdit->setText(browseDir);//set browseEdit text to selected directory
        file = browseDir.toStdString();
        boost::filesystem::path p(file.c_str());//use boost filesystem to make path variable
    }
    
    M 1 Reply Last reply 4 Jan 2016, 10:14
    0
    • V Offline
      V Offline
      ValentinMichelet
      wrote on 4 Jan 2016, 10:13 last edited by
      #2

      Hi,

      What version of Boost are you using?
      Is 'file' variable a std::string?
      You should print some values to see what really happens:

      qDebug() << browseDir;
      std::cout << file << std::endl;
      std::cout << file.c_str() << std::endl;
      

      Also, do you have a minimal code to test it?

      1 Reply Last reply
      0
      • G Galvanic_Cell
        4 Jan 2016, 10:05

        Trying to use boost with Qt and having a problem with filesystem::path, I'm using QFileDialog::getExistingDirectory to return the directory I want to use and store it in a QString, then convert the QString with .toStdString() and store it in a string and then use that string with boost::filesystem::path.

        The problem is that the program crashes when boost::filesystem::path p(file.c_str()); executes, the program crashes. If I comment out that line, everything is fine. Not sure whats going on.

        #include <QDebug>
        #include <QFileDialog>
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <boost/filesystem.hpp>
        #include <boost/filesystem/path.hpp>
        #include <iostream>
        #include <cstring>
        
        void MainWindow::onBrowse()
        {
            //open file select and set selected directory equal to browseDir
            browseDir = QFileDialog::getExistingDirectory(this, "Choose Working Directory", "/home", QFileDialog::ShowDirsOnly);
            ui->browseEdit->setText(browseDir);//set browseEdit text to selected directory
            file = browseDir.toStdString();
            boost::filesystem::path p(file.c_str());//use boost filesystem to make path variable
        }
        
        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 4 Jan 2016, 10:14 last edited by
        #3

        @Galvanic_Cell
        Hi
        if you do
        boost::filesystem::path p(browseDir.toStdString().c_str());
        does it still crash?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          ValentinMichelet
          wrote on 4 Jan 2016, 10:42 last edited by
          #4

          I tried this:

          #include <QDebug>
          #include <QFileDialog>
          #include <boost/filesystem.hpp>
          #include <boost/filesystem/path.hpp>
          
          MainWindow::MainWindow(QWidget* parent):
            QWidget(parent) {
          
            auto browseDir = QFileDialog::getExistingDirectory(this, "Choose Working Directory", "/home", QFileDialog::ShowDirsOnly);
            auto file = browseDir.toStdString();
            boost::filesystem::path p(file.c_str());
            qDebug() << browseDir;
            qDebug() << QString::fromStdString(file);
            qDebug() << file.c_str();
          }
          

          And everything worked fine.
          Could you provide us with a minimal crashing code?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Galvanic_Cell
            wrote on 4 Jan 2016, 15:47 last edited by Galvanic_Cell 1 Apr 2016, 15:52
            #5

            Thanks for the responses, I really appreciate it. This is my first attempt at making a Qt GUI program from scratch and not following a tutorial or a book example.

            Just to make sure I had the boost:filesystem library built correctly, I made a super duper simple program on MSVC 2010 that consisted only of

            // boostfilesystemtest.cpp : Defines the entry point for the console application.
            //
            
            #include "stdafx.h"
            #include <boost\filesystem.hpp>
            #include <iostream>
            
            using namespace std;
            
            
            int _tmain(int argc, _TCHAR* argv[])
            {
            
            	boost::filesystem::path p("C:\c++");
            	std::cout<<"HELLO WORLD";
            	return 0;
            }
            
            

            And ran fine.

            So the kit I am using is Qt Desktop 5.5.1 MSVC 2010 32 bit.
            My Pro file looks like

            QT       += core gui
            
            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
            
            TARGET = KimSort
            TEMPLATE = app
            INCLUDEPATH += C:\boost\boost_1_59_0\boost-dir\include
            LIBS += -L"C:\boost\boost_1_59_0\boost-dir\lib" -lboost_filesystem-vc100-mt-1_59
            SOURCES += main.cpp\
                    mainwindow.cpp
            
            HEADERS  += mainwindow.h
            
            FORMS    += mainwindow.ui
            

            The mainwindow.h header file looks like.

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            #include <QMessageBox>
            #include <string>
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            private:
                Ui::MainWindow *ui;
                QString browseDir;
                void sort();
                void makeGif();
                QMessageBox errorBox;
                std::string file;
            
            
            private slots:
                void onBrowse();
                void onRun();
            };
            
            #endif // MAINWINDOW_H
            
            

            The relevant parts of the mainwindow.cpp source file consists of

            #include <QDebug>
            #include <QFileDialog>
            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <boost/filesystem.hpp>
            #include <boost/filesystem/path.hpp>
            #include <iostream>
            #include <cstring>
            //#include <boost/system/error_code.hpp>
            
            //namespace fs = boost::filesystem;
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                ui->browseEdit->setReadOnly(true);
            
                connect(ui->browseButton, &QPushButton::clicked, this, &MainWindow::onBrowse);
                connect(ui->runButton, &QPushButton::clicked, this, &MainWindow::onRun);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            void MainWindow::onBrowse()
            {
                //open file select and set selected directory equal to browseDir
                browseDir = QFileDialog::getExistingDirectory(this, "Choose Working Directory",
                                                              "/home", QFileDialog::ShowDirsOnly);
                ui->browseEdit->setText(browseDir);//set browseEdit text to selected directory
                file = browseDir.toStdString();
                qDebug()<<"browseDir: "<<browseDir<<"\n";
                qDebug()<<"file: "<<file.c_str()<<"\n";
                boost::filesystem::path p(file.c_str());//use boost filesystem to make path variable IF THIS IS COMMENTED OUT, PROGRAM RUNS WITH NO ERRORS
            }
            
            void MainWindow::onRun()
            {
                if(browseDir.trimmed().isEmpty())
                {
                    errorBox.critical(0, "Usage Error", "Select a Directory Before Running");
                    errorBox.setFixedSize(400,200);
                }
            
                qDebug()<<file.c_str();//outputs the selected directory 
            
            }
            

            Ive omitted some function definitions that are irrelevant.
            Again, if i edit out

            boost::filesystem::path p(file.c_str());
            

            then the program does not crash

            Thanks for any help

            K 1 Reply Last reply 4 Jan 2016, 16:15
            0
            • V Offline
              V Offline
              ValentinMichelet
              wrote on 4 Jan 2016, 15:57 last edited by ValentinMichelet 1 Apr 2016, 16:00
              #6

              I see nothing wrong here. Instead of comment/uncomment this line, try to run it in debug and see in the call stack where it crashes.
              If the debugger still points you to that particular line, I suggest you remove your build and recompile it. And then if this still doesn't work, maybe try to reinstall boost.
              It's really hard to tell what's the matter here. I hope the debug will output something useful.

              1 Reply Last reply
              0
              • G Galvanic_Cell
                4 Jan 2016, 15:47

                Thanks for the responses, I really appreciate it. This is my first attempt at making a Qt GUI program from scratch and not following a tutorial or a book example.

                Just to make sure I had the boost:filesystem library built correctly, I made a super duper simple program on MSVC 2010 that consisted only of

                // boostfilesystemtest.cpp : Defines the entry point for the console application.
                //
                
                #include "stdafx.h"
                #include <boost\filesystem.hpp>
                #include <iostream>
                
                using namespace std;
                
                
                int _tmain(int argc, _TCHAR* argv[])
                {
                
                	boost::filesystem::path p("C:\c++");
                	std::cout<<"HELLO WORLD";
                	return 0;
                }
                
                

                And ran fine.

                So the kit I am using is Qt Desktop 5.5.1 MSVC 2010 32 bit.
                My Pro file looks like

                QT       += core gui
                
                greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                
                TARGET = KimSort
                TEMPLATE = app
                INCLUDEPATH += C:\boost\boost_1_59_0\boost-dir\include
                LIBS += -L"C:\boost\boost_1_59_0\boost-dir\lib" -lboost_filesystem-vc100-mt-1_59
                SOURCES += main.cpp\
                        mainwindow.cpp
                
                HEADERS  += mainwindow.h
                
                FORMS    += mainwindow.ui
                

                The mainwindow.h header file looks like.

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                #include <QMessageBox>
                #include <string>
                
                namespace Ui {
                class MainWindow;
                }
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();
                
                private:
                    Ui::MainWindow *ui;
                    QString browseDir;
                    void sort();
                    void makeGif();
                    QMessageBox errorBox;
                    std::string file;
                
                
                private slots:
                    void onBrowse();
                    void onRun();
                };
                
                #endif // MAINWINDOW_H
                
                

                The relevant parts of the mainwindow.cpp source file consists of

                #include <QDebug>
                #include <QFileDialog>
                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                #include <boost/filesystem.hpp>
                #include <boost/filesystem/path.hpp>
                #include <iostream>
                #include <cstring>
                //#include <boost/system/error_code.hpp>
                
                //namespace fs = boost::filesystem;
                
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                
                    ui->browseEdit->setReadOnly(true);
                
                    connect(ui->browseButton, &QPushButton::clicked, this, &MainWindow::onBrowse);
                    connect(ui->runButton, &QPushButton::clicked, this, &MainWindow::onRun);
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                void MainWindow::onBrowse()
                {
                    //open file select and set selected directory equal to browseDir
                    browseDir = QFileDialog::getExistingDirectory(this, "Choose Working Directory",
                                                                  "/home", QFileDialog::ShowDirsOnly);
                    ui->browseEdit->setText(browseDir);//set browseEdit text to selected directory
                    file = browseDir.toStdString();
                    qDebug()<<"browseDir: "<<browseDir<<"\n";
                    qDebug()<<"file: "<<file.c_str()<<"\n";
                    boost::filesystem::path p(file.c_str());//use boost filesystem to make path variable IF THIS IS COMMENTED OUT, PROGRAM RUNS WITH NO ERRORS
                }
                
                void MainWindow::onRun()
                {
                    if(browseDir.trimmed().isEmpty())
                    {
                        errorBox.critical(0, "Usage Error", "Select a Directory Before Running");
                        errorBox.setFixedSize(400,200);
                    }
                
                    qDebug()<<file.c_str();//outputs the selected directory 
                
                }
                

                Ive omitted some function definitions that are irrelevant.
                Again, if i edit out

                boost::filesystem::path p(file.c_str());
                

                then the program does not crash

                Thanks for any help

                K Offline
                K Offline
                koahnig
                wrote on 4 Jan 2016, 16:15 last edited by
                #7

                @Galvanic_Cell said:

                Thanks for the responses, I really appreciate it. This is my first attempt at making a Qt GUI program from scratch and not following a tutorial or a book example.

                Just to make sure I had the boost:filesystem library built correctly, I made a super duper simple program on MSVC 2010 that consisted only of

                // boostfilesystemtest.cpp : Defines the entry point for the console application.
                //
                
                #include "stdafx.h"
                #include <boost\filesystem.hpp>
                #include <iostream>
                
                using namespace std;
                
                
                int _tmain(int argc, _TCHAR* argv[])
                {
                
                	boost::filesystem::path p("C:\c++");
                	std::cout<<"HELLO WORLD";
                	return 0;
                }
                

                Are you sure that your test does what you expect?
                The back slash should be escaped.

                Also sometimes it make sense to do a complete rebuild.

                Vote the answer(s) that helped you to solve your issue(s)

                V 1 Reply Last reply 5 Jan 2016, 08:13
                0
                • K koahnig
                  4 Jan 2016, 16:15

                  @Galvanic_Cell said:

                  Thanks for the responses, I really appreciate it. This is my first attempt at making a Qt GUI program from scratch and not following a tutorial or a book example.

                  Just to make sure I had the boost:filesystem library built correctly, I made a super duper simple program on MSVC 2010 that consisted only of

                  // boostfilesystemtest.cpp : Defines the entry point for the console application.
                  //
                  
                  #include "stdafx.h"
                  #include <boost\filesystem.hpp>
                  #include <iostream>
                  
                  using namespace std;
                  
                  
                  int _tmain(int argc, _TCHAR* argv[])
                  {
                  
                  	boost::filesystem::path p("C:\c++");
                  	std::cout<<"HELLO WORLD";
                  	return 0;
                  }
                  

                  Are you sure that your test does what you expect?
                  The back slash should be escaped.

                  Also sometimes it make sense to do a complete rebuild.

                  V Offline
                  V Offline
                  ValentinMichelet
                  wrote on 5 Jan 2016, 08:13 last edited by
                  #8

                  @koahnig if the back slash were an issue, it wouldn't even compile, right?

                  K 1 Reply Last reply 5 Jan 2016, 09:56
                  0
                  • V ValentinMichelet
                    5 Jan 2016, 08:13

                    @koahnig if the back slash were an issue, it wouldn't even compile, right?

                    K Offline
                    K Offline
                    koahnig
                    wrote on 5 Jan 2016, 09:56 last edited by
                    #9

                    @ValentinMichelet
                    I guess that the single backslash and 'c' will be comined to some phony character which is possibly valid.
                    Never used the boost filesystem routines and therefore do not know what this contructor actually does. You may want to check the small test with a valid path. Basically I just saw the issue and thought to note it.

                    Your problem looks quite strange to me. The current state seems to be a bit of trial and error only. Probably you had tried already, but "rebuild all" for the project sometimes solves problems "magically".

                    Vote the answer(s) that helped you to solve your issue(s)

                    V G 2 Replies Last reply 5 Jan 2016, 12:02
                    0
                    • K koahnig
                      5 Jan 2016, 09:56

                      @ValentinMichelet
                      I guess that the single backslash and 'c' will be comined to some phony character which is possibly valid.
                      Never used the boost filesystem routines and therefore do not know what this contructor actually does. You may want to check the small test with a valid path. Basically I just saw the issue and thought to note it.

                      Your problem looks quite strange to me. The current state seems to be a bit of trial and error only. Probably you had tried already, but "rebuild all" for the project sometimes solves problems "magically".

                      V Offline
                      V Offline
                      ValentinMichelet
                      wrote on 5 Jan 2016, 12:02 last edited by ValentinMichelet 1 May 2016, 12:03
                      #10

                      @koahnig
                      Even if the syntax was correct, the include would fail when trying to find the library, resulting in a compile error.

                      But I agree with you, a rebuild may solve this problem.

                      K 1 Reply Last reply 5 Jan 2016, 13:26
                      0
                      • K koahnig
                        5 Jan 2016, 09:56

                        @ValentinMichelet
                        I guess that the single backslash and 'c' will be comined to some phony character which is possibly valid.
                        Never used the boost filesystem routines and therefore do not know what this contructor actually does. You may want to check the small test with a valid path. Basically I just saw the issue and thought to note it.

                        Your problem looks quite strange to me. The current state seems to be a bit of trial and error only. Probably you had tried already, but "rebuild all" for the project sometimes solves problems "magically".

                        G Offline
                        G Offline
                        Galvanic_Cell
                        wrote on 5 Jan 2016, 12:41 last edited by
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • V ValentinMichelet
                          5 Jan 2016, 12:02

                          @koahnig
                          Even if the syntax was correct, the include would fail when trying to find the library, resulting in a compile error.

                          But I agree with you, a rebuild may solve this problem.

                          K Offline
                          K Offline
                          koahnig
                          wrote on 5 Jan 2016, 13:26 last edited by
                          #12

                          @ValentinMichelet said:

                          Even if the syntax was correct, the include would fail when trying to find the library, resulting in a compile error.

                          This does not fit to what I had responded.
                          ´´´
                          int _tmain(int argc, _TCHAR* argv[])
                          {

                          boost::filesystem::path p("C:\c++");
                          std::cout<<"HELLO WORLD";
                          return 0;
                          

                          }
                          ´´´
                          My reference to escaping was for this small test program published. However, it is was hidden by this forum system.

                          Vote the answer(s) that helped you to solve your issue(s)

                          V 1 Reply Last reply 5 Jan 2016, 13:30
                          0
                          • K koahnig
                            5 Jan 2016, 13:26

                            @ValentinMichelet said:

                            Even if the syntax was correct, the include would fail when trying to find the library, resulting in a compile error.

                            This does not fit to what I had responded.
                            ´´´
                            int _tmain(int argc, _TCHAR* argv[])
                            {

                            boost::filesystem::path p("C:\c++");
                            std::cout<<"HELLO WORLD";
                            return 0;
                            

                            }
                            ´´´
                            My reference to escaping was for this small test program published. However, it is was hidden by this forum system.

                            V Offline
                            V Offline
                            ValentinMichelet
                            wrote on 5 Jan 2016, 13:30 last edited by
                            #13

                            @koahnig
                            Oh, my bad!

                            Anyway, I think that rebuild was the right thing to do.

                            1 Reply Last reply
                            0

                            1/13

                            4 Jan 2016, 10:05

                            • Login

                            • Login or register to search.
                            1 out of 13
                            • First post
                              1/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved