binary.exe 中 0x7650C41F 处的未处理异常:Microsoft C++ 异常:内存位置 0x003EEE00 处的 std::bad_alloc。
First-chance exception at 0x77983AB3 (ntdll.dll) in binary.exe: 0xC0000005: Access violation reading location 0x6F726369.
First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DF0DC.
First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DF0DC.
First-chance exception at 0x77983AB3 (ntdll.dll) in binary.exe: 0xC0000005: Access violation reading location 0x6F726369.
First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DEA40.
First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DEA40.
First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
Unhandled exception at at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DEA40.
The program '[7632] binary.exe' has exited with code 0 (0x0).
我不知道我是否犯了新手错误,但每次我尝试运行下面的代码时,我都会得到上面列出的错误 - 从我可以通过各种论坛帖子和错误消息收集的内容来看,有一个问题内存分配,但就我所知。
下面列出的代码是我项目的缩短版本,因为源文件很长,实际上不需要发布。
int _tmain(int argc, _TCHAR* argv[])
{
check(true);
system("pause");
return 0;
}
int check(bool initialCheck)
{
char* path = getDocumentRootA(); strcat(path, "Test//file.test");
char* filePathA = getDocumentRootA(); strcat(filePathA, "Test2\\file.test");
char* filePathB = getDocumentRootA(); strcat(filePathB, "Test3\\file.test");
cout << "Checking if files exists...";
if (doesFileExist(path) == true)
{
cout << "Yes\n";
} else if (doesFileExist(path) == false) {
cout << "No\n"; // todo
}
cout << "Checking if other files exist...";
if (doesFileExist(filePathA) == true && doesFileExist(filePathB) == true)
{
cout << "Yes\n";
}
return 0;
}
char* getDocumentRootA()
{
CHAR documentRootC[MAX_PATH]; CA2W uDocumentRoot(documentRootC);
HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, uDocumentRoot); CW2A documentRoot_T(uDocumentRoot); strcat(documentRoot_T, "\\");
string documentRootTemp = documentRoot_T; char* documentRoot = const_cast<char*>(documentRootTemp.c_str());
cout<<documentRoot;
return documentRoot;
}
可能还值得注意的是,我试图更改代码的第一部分(参见下面的示例),以便getDocumentRootA()
只调用一次函数,但这并没有解决问题。
char* testvar = getDocumentRootA();
char* path = testvar; strcat(path, "Microsoft\\file.test");
char* filePathA = testvar; strcat(filePathA, "Windows\\AppLoc\\file.test");
char* filePathB = testvar; strcat(filePathB, "Windows\\U\\file.test");