Whenever I run this code, I get Exception thrown at 0x00007FF6CA077375 in console test.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
coming from a file named "xmemory", what it is supposed to do is loop through different folders looking for a specified file, so it uses a bit of recursion, I'm not sure if that's part of the problem, it's still a WIP but I need to do something about the Exception first. I am using Visual Studio 17.3.0, C 20 Standard (/std:c 20) and MinGW gcc compiler, in case that helps
#include <string>
#include <vector>
#include <iostream>
#include <filesystem>
using std::filesystem::directory_iterator;
namespace fs = std::filesystem;
using namespace std;
string endstr(string str, string delim) {
int i = str.length();
while (str[i] != delim[0])
{
if (i == 0) break;
i--;
}
return str.substr(i 1, str.length());
}
string start = "C:\\";
vector<string> explored;
string fl;
string *dir;
string getFile(const char* item, string startPath)
{
for (const auto& file : directory_iterator(startPath))
{
fl = file.path().string();
cout << fl << endl;
if (fl == endstr(item, "\\")) {
return fl;
}
else if (file.is_directory()) {
if (find(explored.begin(), explored.end(), fl) != explored.end()) {
getFile(item, fl);
}
}
else {
break;
}
}
explored.push_back(fl);
}
int main() {
getFile("Terraria.exe", "C:\\");
}
Edit: debeg logs n stuff
Debug
'console test.exe' (Win32): Loaded 'C:\Users\willk\source\repos\console test\x64\Debug\console test.exe'. Symbols loaded.
'console test.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'.
'console test.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'.
'console test.exe' (Win32): Loaded 'C:\Program Files\Avast Software\Avast\aswhook.dll'.
'console test.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'.
'console test.exe' (Win32): Loaded 'C:\Windows\System32\msvcp140d.dll'.
'console test.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'.
'console test.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140_1d.dll'.
'console test.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbased.dll'.
The thread 0x5cec has exited with code 0 (0x0).
Exception thrown at 0x00007FF703A47415 in console test.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Build
Build started...
1>------ Build started: Project: console test, Configuration: Debug x64 ------
1>console test.cpp
1>C:\Users\willk\source\repos\console test\console test\console test.cpp(14,25): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
1>C:\Users\willk\source\repos\console test\console test\console test.cpp(45): warning C4715: 'getFile': not all control paths return a value
1>console test.vcxproj -> C:\Users\willk\source\repos\console test\x64\Debug\console test.exe
1>Done building project "console test.vcxproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
CodePudding user response:
What goes wrong here is that getFile
returns a bad string (uninitialized/garbage due to not returning any string) after it reaches explored.push_back(fl);
. Then back in main
, that string is destroyed, but that fails, so you're seeing an access violation coming out of the deep parts of the implementation of the destructor of basic_string
.
In general, not returning something from a function that is supposed to return something, is a recipe for Bad Things happening.
While "debug output" rarely contains anything useful (unless you've put it there yourself), stack trace is generally useful because it helps you pin-point what the program was doing when it died: