C++ - Ausnahme: bad_alloc at memory location

Unbehandelte Ausnahme bei 0x7650C41F in binary.exe: Microsoft C++ - Ausnahme: std::bad_alloc at memory location 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).

Ich weiß nicht, ob ich ein newbie Fehler, aber jedes mal, wenn ich versuche, führen Sie den code unten bekomme ich die Fehler, die oben aufgeführt sind - von dem, was ich sammeln können durch verschiedene Beiträge im forum und der Fehlermeldungen, gibt es ein Problem mit der Zuweisung von Speicher, aber das ist so weit wie ich habe.

Den unten aufgeführten code ist eine verkürzte version von meinem Projekt, wie die Quell-Dateien sind ziemlich lang und nicht wirklich brauchen, um veröffentlicht werden.

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;
}

Es ist wohl auch erwähnenswert, dass ich habe versucht, ändern Sie den ersten Teil des Codes (siehe Beispiel unten), so dass die getDocumentRootA() Funktion nur einmal aufgerufen, aber das hat nicht das Problem zu lösen.

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");
  • Da Sie erwähnen, könnten Sie ein Neuling sind, möchte ich darauf hinweisen, sollten Sie eigentlich nur eine Anweisung pro Zeile. Wenn mehr als eine Anweisung (irgendetwas gefolgt von einem Semikolon, ist wirklich schlecht: es macht den code schwerer zu Lesen und zu Debuggen ein Alptraum.
InformationsquelleAutor Ryan | 2013-03-28
Schreibe einen Kommentar