C++ Exception: bad_alloc at memory location

Ryan picture Ryan · Mar 28, 2013 · Viewed 10.4k times · Source

Unhandled exception at at 0x7650C41F in binary.exe: Microsoft C++ exception: 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).

I don't know if I'm making a newbie mistake, but every time I try to run the code below I get the errors listed above - from what I can gather through various forum posts and the error messages, there is an issue with the allocation of memory but that's as far as i've got.

The code listed below is a shortened version of my project, as the source files are quite long and don't really need to be posted.

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

It's probably also worth noting that I attempted to change the first part of the code (see example below) so that the getDocumentRootA() function was only called once, but that didn't solve the issue.

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

Answer

Shafik Yaghmour picture Shafik Yaghmour · Mar 28, 2013

In getDocumentRootA() you are retuning a pointer to a member variable of a stack allocated object which is bad since once you leave the function it will be cleaned up. It is even worse since you should not be modifying the internal member of string so there are two problems there:

  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

and here is where you are using that pointer:

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

You should probably just return a std::string from GetDocumentRootA() and then in check using c_str in the places where you need const char *. You can use the += operator to append char * to a std::string, see this reference. Here is a very basic example, to make my suggestion more concrete:

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