1
votes

getting this error in an application written in C++ (VS 2010):

Unhandled exception at 0x77648da9 in divt.exe: 0xC0000005: Access violation writing location 0x00000014.

it points to this function in free.c:

void __cdecl _free_base (void * pBlock)
{

        int retval = 0;


        if (pBlock == NULL)
            return;

        RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));

        retval = HeapFree(_crtheap, 0, pBlock);
        if (retval == 0) //<-----------------------right here
        {
            errno = _get_errno_from_oserr(GetLastError());
        }
}

Via debugging I was able to determine where its actually crashing:

void MenuState::LoadContentFromFile(char* File,std::string &Content)
{
    std::string strbuf;

    char buffer[1028];

    std::fstream file;
    file.open(File,std::ios_base::in);

    if(file.fail()) 
    {
        Content = ErrorTable->GetString("W0001");
        return;
    }
    if(file.is_open())
    {
        while(!file.eof())
        {
            file.getline(buffer,128,'\n');  // <----here
            strbuf = buffer;
            Content += strbuf + "\n";
        }
    }

    file.close();

    strbuf.clear();
}

It crashes on file.getline(buffer,128,'\n');

I don't understand why but it's only doing it in release build (Optimizations turned off), on debug build its working fine.

Any Ideas?

3
You might have a stack overflow, check your stack size. Do you really need the buffer to be 1028, if you only read 128 bytes into it?littleadv

3 Answers

3
votes

I know this is an old question, but when you encounter these sorts of issues buried deep in files such as, free.c or xmemory, you may also want to double check your project configuration. Especially when the issue pertains to only certain build configurations.

For example, in MSVC check your Project Properties > Configuration Properties > C/C++ > Code Generation > Runtime Library. Make sure it consistent for all dependencies and that it is set to a Debug/Release variant depending on the current build.

1
votes

I would bet that the read prior to the read crashing the application actually failed (although I'm not quite sure why it would crash). The important thing to note is that eof() is only good for determining what caused a read failure (and typically suppressing an error message). In addition, you always want to check after the read whether it was successful. Finally, I can't see any reason why you don't read an std::string directly. In summary, try to use this loop instead:

for (std::string strbuf; std::getline(file, strbuf); ) {
    Content += strbuf;
}
0
votes

Asked a friend for help, we came up with this Solution:

std::string strbuf;

char buffer[256] = "\0";

FILE* f = fopen(File, "rt");

while(fgets(buffer,sizeof(buffer),f) != NULL)
{
    Content += buffer;
}

fclose(f);
strbuf.clear();

Works fine, still thanks for your efforts.