boost::filesystem::exists linux 上的段错误
boost::filesystem::exists segfault on linux
函数 boost::filesystem::exists() 在我的 Kubuntu 上出现段错误,我不知道为什么。
这是我的函数:
if (!boost::filesystem::exists("./sthg"))
{
// ....
}
我检查了 valgrind,他告诉我错误在 xstat.c:35 中,错误是 "Syscall param (file_name) contains uninitialised byte(s)."
这是我的编译行:
g++ ... -o ... -lboost_system -lboost_filesystem
编辑 1:
调用创建目录函数的函数:
void TCPConnection::enable(void)
{
try {
StaticTools::CreateFolder("./clients");
} catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
}
}
创建目录的函数:
void StaticTools::CreateFolder(std::string const& path)
{
if (!boost::filesystem::exists(path)) {
if (!boost::filesystem::create_directory(path)) {
throw (std::runtime_error("..."));
}
}
}
Valgrind 日志:
http://pastebin.com/gbzFDDNg
1) 如果 boost::filesystem 的文档非常不清楚意味着我认为它的意思,那么创建目录的函数应该简单地读取
void StaticTools::CreateFolder(std::string const& path)
{
boost::filesystem::create_directory(path);
}
因为,如果create_directory
returns没有抛出异常,那么目录确实存在。 (return 值仅告诉您目录 刚刚创建 还是 已经作为目录存在 。您可能不知道关心。)
2) 如果对这个函数的调用真的是
StaticTools::CreateFolder("./clients");
以字符串文字“./clients”作为参数,和 "Syscall param stat(file_name) contains uninitialised byte(s)"确实是首先 valgrind
发出的错误,那么,我很遗憾地说,您可能不幸遇到了 boost 与 C++ 运行时库不一致的情况。具体来说,我认为您的 libstdc++.so
可能默认为 C++11 std::string
而您的 libboost_filesystem.so
期望使用 C++98 std::string
,反之亦然。你没有什么好的办法来解决这个问题;负责提升 and/or C++ 运行时的 Kubuntu 人员必须这样做。最省力的方法可能是停止使用 boost。这不是开玩笑。
函数 boost::filesystem::exists() 在我的 Kubuntu 上出现段错误,我不知道为什么。
这是我的函数:
if (!boost::filesystem::exists("./sthg"))
{
// ....
}
我检查了 valgrind,他告诉我错误在 xstat.c:35 中,错误是 "Syscall param (file_name) contains uninitialised byte(s)."
这是我的编译行:
g++ ... -o ... -lboost_system -lboost_filesystem
编辑 1:
调用创建目录函数的函数:
void TCPConnection::enable(void)
{
try {
StaticTools::CreateFolder("./clients");
} catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
}
}
创建目录的函数:
void StaticTools::CreateFolder(std::string const& path)
{
if (!boost::filesystem::exists(path)) {
if (!boost::filesystem::create_directory(path)) {
throw (std::runtime_error("..."));
}
}
}
Valgrind 日志: http://pastebin.com/gbzFDDNg
1) 如果 boost::filesystem 的文档非常不清楚意味着我认为它的意思,那么创建目录的函数应该简单地读取
void StaticTools::CreateFolder(std::string const& path)
{
boost::filesystem::create_directory(path);
}
因为,如果create_directory
returns没有抛出异常,那么目录确实存在。 (return 值仅告诉您目录 刚刚创建 还是 已经作为目录存在 。您可能不知道关心。)
2) 如果对这个函数的调用真的是
StaticTools::CreateFolder("./clients");
以字符串文字“./clients”作为参数,和 "Syscall param stat(file_name) contains uninitialised byte(s)"确实是首先 valgrind
发出的错误,那么,我很遗憾地说,您可能不幸遇到了 boost 与 C++ 运行时库不一致的情况。具体来说,我认为您的 libstdc++.so
可能默认为 C++11 std::string
而您的 libboost_filesystem.so
期望使用 C++98 std::string
,反之亦然。你没有什么好的办法来解决这个问题;负责提升 and/or C++ 运行时的 Kubuntu 人员必须这样做。最省力的方法可能是停止使用 boost。这不是开玩笑。