【发布时间】:2018-02-13 11:41:09
【问题描述】:
boost::filesystem::exists(pathName) 有问题。
当文件夹不存在时,该方法按预期返回false。
如果文件夹存在,它将在 Boost 文件 operations.cpp 的 make_permissions(const path& p, DWORD attr) 方法中引发异常。
这是该文件中的代码
if (equal_string_ordinal_ic(ext.c_str(), L".exe")
|| equal_string_ordinal_ic(ext.c_str(), L".com")
|| equal_string_ordinal_ic(ext.c_str(), L".bat")
|| equal_string_ordinal_ic(ext.c_str(), L".cmd"))
prms |= fs::owner_exe | fs::group_exe | fs::others_exe;
return prms;
消息是:
Exception thrown at 0x00000000 in LineSync.exe: 0xC0000005: Access violation executing location 0x00000000.
这是在if 语句中抛出的。
我在 Windows 10 下的 MFC 应用程序中使用此方法。在 Windows 7 上,问题不存在。
如果我在 Windows 10 上创建示例 Windows 控制台应用程序,问题也没有发生。
这是不适用于 MFC 项目但适用于 Windows 10 控制台应用程序的代码。
boost::filesystem::path pathDir(L"logs");
if (!boost::filesystem::exists(pathDir)) {
boost::filesystem::create_directory(pathDir);
}
更改为 boost::filesystem::is_directory(pathDir) 也确实导致 MFC 项目出现异常
MFC 和 Windows 10 是否存在问题? 或者有没有更好的方法来检查特定目录是否存在?
编辑 2:
这是一个示例,它会在使用 Boost 1.66 的 Windows 上导致错误
bool dummy = boost::filesystem::exists("logs");
class Demo {
public:
~Demo() {
std::cout << boost::filesystem::path("logs").string() << std::endl;
}
};
Demo d;
int main()
{
Demo();
system("PAUSE");
return 0;
}
【问题讨论】:
-
错误不在Boost中,它在你的代码中,你没有向我们展示的代码。您如何使用 Boost 库以及如何构建它也可能是一个问题。请花一些时间到read about how to ask good questions,并尝试创建一个Minimal, Complete, and Verifiable Example 向我们展示。我也推荐你learn how to debug your programs。
-
消息中显示的地址强烈表明存在某种空指针。
-
仅仅因为在特定行上引发了这种异常并不意味着这就是错误或问题所在。问题可能出现在您代码中的任何其他位置,这就是为什么在任何人都可以给您答案之前需要重现问题的minimal reproducible example。
-
是的,就像 G.M 在他的评论中提到的那样