Reputation: 41
i'm getting error:Debug Error R6010 -abort() has been called
in my code :
bool ListFiles(wstring path, wstring mask, vector<wstring>& files) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
stack<wstring> directories;
directories.push(path);
files.clear();
while (!directories.empty()) {
path = directories.top();
spec = path + L"\\" + mask;
directories.pop();
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
if (wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push(path + L"/" + ffd.cFileName);
}
else {
files.push_back(path + L"/" + ffd.cFileName);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;}
void findText(std::string filename, std::string word , promise<string> &prom) {
std::ifstream f(filename);
std::string s;
std::string notFound = "no";
bool found = false;
if (!f) {
std::cout << "il file non esiste"<<std::endl;
}
while (f.good()) {
std::getline(f, s);
if (s.find(word, 0) != string::npos) {
found = true;
}
}
if (found) {
//cout << "Parola trovata in -> " << filename << endl;
prom.set_value_at_thread_exit(filename);
}
else {
prom.set_value_at_thread_exit(notFound);
}
f.close();}
int main(int argc, char* argv[]){
//vector<std::thread> th;
vector<future<string>> futures;
vector<wstring> files;
string search = "ciao";
string notFound = "no";
if (ListFiles(L"pds", L"*", files)) {
for (vector<wstring>::iterator it = files.begin(); it != files.end(); ++it) {
//wcout << it->c_str() << endl;
wstring ws(it->c_str());
string str(ws.begin(), ws.end());
// Show String
//cout << str << endl;
//Creo una promise per ogni thread in cui andrò a cercare il risulato
std::promise<string> prom;
futures.push_back(prom.get_future());
std::thread(findText,str,search,std::ref(prom)).detach();
}
}
for (int i = 0; i < futures.size(); i++){
futures.at(i).wait();
if (futures.at(i).get().compare(notFound)!=0)
cout << "Parola trovata in ->" <<futures.at(i).get()<<endl;
}
return 0;}
I've tried before without using promises and making each thread printing the name of file if word was found and it worked. So i don't know why using promises and future to retrive this value cause me this problem... I'm using VS 2013
Upvotes: 0
Views: 3752
Reputation: 409156
Lets take a close look at these lines lines:
for (...) {
...
std::promise<string> prom;
...
std::thread(findText,str,search,std::ref(prom)).detach();
}
First you create a local variable prom
, and then you pass a reference to that variable to the thread.
The problem with this is that once the loop iterates, the variable prom
goes out of scope and the object is destructed. The reference you once had no longer have anything it references. Using the reference will lead to undefined behavior.
So the solution is to not use references (or pointers to the prom
variable), which leads to problems because std::promise
can't be copied. It can, however, be moved:
std::thread(findText,str,search,std::move(prom)).detach();
Fir this you might need to make the thread function take the promise argument as a rvalue reference:
void findText(std::string filename, std::string word , promise<string> &&prom) {
...
}
If the above solution doesn't work, then you could use dynamic allocation using the new C++11 smart pointers like std::unique_ptr
.
Then the thread function should take the smart pointer by value, like
void findText(std::string filename, std::string word , std::unique_ptr<std::promise<string>> prom) {
...
}
And you create the thread like
auto prom = std::make_unique<std::promise<std::string>>();
// Or if you don't have std::make_unique
// std::unique_ptr<std::promise<std::string>> prom(new std::promise<std::string>);
futures.push_back(prom->get_future();
std::thread(findText,str,search,std::move(prom)).detach();
Remember that in your thread function (findText
) the variable prom
is a pointer and you need to use the arrow operator when using it, like e.g.
prom->set_value_at_thread_exit(filename);
// ^^
// Note "arrow" operator here
Upvotes: 4