C ++异常:内存位置的bad_alloc

时间:2013-03-28 18:45:28

标签: c++ exception memory visual-studio-2012 bad-alloc

  

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

1 个答案:

答案 0 :(得分:3)

getDocumentRootA()中,您正在重新指定一个指向堆栈分配对象的成员变量的指针,因为一旦您离开该函数,它将被清除。更糟糕的是,你不应该修改string的内部成员,因此存在两个问题:

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

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


   return 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,请参阅此reference。这是一个非常基本的例子,让我的建议更加具体:

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