C ++ Исключение: bad_alloc в ячейке памяти

Необработанное исключение в 0x7650C41F в двоичном файле.exe: исключение Microsoft C ++: std :: bad_alloc в расположении памяти 0x003EEE00.

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");

1

Решение

В getDocumentRootA() вы перенастраиваете указатель на переменную-член выделенного объекта стека, что плохо, так как после выхода из функции он будет очищен. Это еще хуже, так как вы не должны изменять внутренний элемент string поэтому есть две проблемы:

  string documentRootTemp = documentRoot_T;
^^^^^^
Object on the stack

char* documentRoot = const_cast<char*>(documentRootTemp.c_str());
^^^^^^
Pointer to said objectreturn documentRoot;
^^^^^
Returning said pointer

и вот где вы используете этот указатель:

  char* path = getDocumentRootA();
strcat(path, "Test//file.test");
^^^^
Modifying pointer to object that does not exist anymore

Вы должны, вероятно, просто вернуть std::string от GetDocumentRootA() а затем в check с помощью c_str в тех местах, где вам нужно const char *, Вы можете использовать += оператор для добавления char * к std::string, видеть это ссылка. Вот очень простой пример, чтобы сделать мое предложение более конкретным:

#include <string>
#include <iostream>

std::string getDocumentRootA()
{
std::string str( "Path//") ;

return str ;
}

bool doesFileExist( const char *p )
{
bool ret = false ;

// Do checking here

return ret  ;
}

int main()
{
std::string str2( getDocumentRootA() ) ;

str2 += "Test//file.test" ;

std::cout << str2.c_str()  << std::endl ;

if( doesFileExist( str2.c_str() ))
{
std::cout << "Yes" << std::endl ;
}
else
{
std::cout << "No" << std::endl ;
}
}
3

Другие решения

Других решений пока нет …