"Access violation writing location" with file.getline? (Only in release build)

1

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?

c++
visual-studio-2010
access-violation
asked on Stack Overflow Jan 3, 2012 by bandrewk • edited May 4, 2012 by kapa

3 Answers

3

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.

answered on Stack Overflow Dec 31, 2012 by user1556435
1

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;
}
answered on Stack Overflow Jan 3, 2012 by Dietmar Kühl
0

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.

answered on Stack Overflow Jan 4, 2012 by bandrewk

User contributions licensed under CC BY-SA 3.0