nolimitee nolimitee - 3 months ago 118
C++ Question

boost::filesystem::exists segfault on linux

The function boost::filesystem::exists() segfault on my Kubuntu and I don't know why.

Here is my function:

if (!boost::filesystem::exists("./sthg"))
{
// ....
}


I check with valgrind and he tells me the error is in xstat.c:35 and the error is "Syscall param (file_name) contains uninitialised byte(s)."

Here is my compilation line:

g++ ... -o ... -lboost_system -lboost_filesystem


EDIT 1:

the function that calls the function that creates the directory:

void TCPConnection::enable(void)
{
try {
StaticTools::CreateFolder("./clients");
} catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
}
}


the function that creates the directory:

void StaticTools::CreateFolder(std::string const& path)
{
if (!boost::filesystem::exists(path)) {
if (!boost::filesystem::create_directory(path)) {
throw (std::runtime_error("..."));
}
}
}


Valgrind log:
http://pastebin.com/gbzFDDNg

Answer

1) If boost::filesystem's exceedingly unclear documentation means what I think it means, then the function that creates the directory should simply read

void StaticTools::CreateFolder(std::string const& path)
{
    boost::filesystem::create_directory(path);
}

because, if create_directory returns without throwing an exception, then the directory does exist. (The return value only tells you whether the directory has just been created versus already existed as a directory. You probably don't care.)

2) If the call to this function really truly was

    StaticTools::CreateFolder("./clients");

with the string literal "./clients" as the argument, and "Syscall param stat(file_name) contains uninitialised byte(s)" really truly was the very first error emitted by valgrind, then, I regret to say, you may be in the unfortunate case of boost being inconsistent with the C++ runtime library. Specifically, I think your libstdc++.so may be defaulting to C++11 std::string whereas your libboost_filesystem.so is expecting C++98 std::string, or vice versa. There is no good way for you to fix this; the Kubuntu people responsible for boost and/or the C++ runtime have to do it. The path of least effort is probably to stop using boost. That is not a joke.

Comments