我有一些代码,从boost :: thread开始。该线程在某个时刻被中断。
线程代码:
void CLifeSimLauncherDlg::hashFilesThread() {
wstring *pathToMainDir = new wstring(L"./LifeSimGame");
boost::this_thread::interruption_point();
MyLib::hashFilesInDirExternal(doc, pathToMainDir,
cpr);
}
功能hashFilesInDirExternal
调用另一个函数hashFilesInDir
void FileIO::hashFilesInDir(wstring dirn, pugi::xml_node *root,
void(*calcHashFunction)(wstring, wstring, uintmax_t, pugi::xml_node*))
{
try {
long long int folderSize = 0;
boost::replace_all(dirn, "\\\\", "\\");
boost::replace_all(dirn, "\\", "/");
boost::filesystem::path folderPath(dirn);
if (boost::filesystem::exists(folderPath)) {
boost::filesystem::directory_iterator end_itr;
for (boost::filesystem::directory_iterator dirIte(dirn); dirIte != end_itr; ++dirIte)
{
boost::filesystem::path filePath(dirIte->path());
try {
if (boost::filesystem::is_directory(dirIte->status()))
{
hashFilesInDir(MyStrings::StringToWString(dirIte->path().string()), root, calcHashFunction);
}
else {
try {
(*calcHashFunction)(
MyStrings::StringToWString(dirIte->path().string()),
MyStrings::StringToWString(to_string(boost::filesystem::file_size(dirIte->path()))),
boost::filesystem::file_size(dirIte->path()),
root
);
}
catch (exception ex) {
MessageBox(nullptr, L"Some error occured on file hashing2.", L"Error:", MB_OK);
}
}
}
catch (exception& e) {
cout << e.what() << endl;
}
}
}
}
catch (...) {
MessageBox(nullptr, L"Some error occured on file indexing.", L"Error:", MB_OK);
}
}
当线程中断时,程序因访问冲突异常而失败:
Exception thrown at 0x005B3A1C in LifeSimLauncher.Main.exe: 0xC0000005: Access violation executing location 0x005B3A1C.
在path.hpp,第412行,包含代码
path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(),
tmp);
此代码位于
中# ifdef BOOST_WINDOWS_API
const std::string string() const
{
std::string tmp;
if (!m_pathname.empty())
/*This is 412 line*/ path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(),
tmp);
return tmp;
}
我做错了什么以及如何防止此异常?