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

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.
  • ValentinMicheletV Offline
    ValentinMicheletV Offline
    ValentinMichelet
    wrote on 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 last edited by Galvanic_Cell
      #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
      0
      • ValentinMicheletV Offline
        ValentinMicheletV Offline
        ValentinMichelet
        wrote on last edited by ValentinMichelet
        #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

          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 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)

          ValentinMicheletV 1 Reply Last reply
          0
          • K koahnig

            @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.

            ValentinMicheletV Offline
            ValentinMicheletV Offline
            ValentinMichelet
            wrote on last edited by
            #8

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

            K 1 Reply Last reply
            0
            • ValentinMicheletV ValentinMichelet

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

              K Offline
              K Offline
              koahnig
              wrote on 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)

              ValentinMicheletV G 2 Replies Last reply
              0
              • K koahnig

                @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".

                ValentinMicheletV Offline
                ValentinMicheletV Offline
                ValentinMichelet
                wrote on last edited by ValentinMichelet
                #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
                0
                • K koahnig

                  @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 last edited by
                  #11
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • ValentinMicheletV ValentinMichelet

                    @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 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)

                    ValentinMicheletV 1 Reply Last reply
                    0
                    • K koahnig

                      @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.

                      ValentinMicheletV Offline
                      ValentinMicheletV Offline
                      ValentinMichelet
                      wrote on last edited by
                      #13

                      @koahnig
                      Oh, my bad!

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

                      1 Reply Last reply
                      0

                      • Login

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