Using boost::filesystem::path causes program to crash
-
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 }
-
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?
-
@Galvanic_Cell
Hi
if you do
boost::filesystem::path p(browseDir.toStdString().c_str());
does it still crash? -
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? -
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 likeQT += 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 outboost::filesystem::path p(file.c_str());
then the program does not crash
Thanks for any help
-
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. -
@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.
-
@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".
-
This post is deleted!
-
@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.