简体   繁体   中英

ofstream::write() unhandled exception

So I'm writing a logging library for logging all sorts of things, and when I ran a test on it, it kept on crashing. I narrowed the exception down to the writing function when I write the log message to the ofstream file. I parse the message and stuff, and then I have the actual call to ofstream::write(). here is the part where I get a reuntime error:

void Logger::writeMessage(LogMessage* message)
{
    if(message==NULL)
        return;
    char buffer[MAX_PATH];
    switch(message->GetMessageType())
    {
    case LOGMESSAGE_HEADER:
        sprintf(buffer, m_logInfo->headerFormat, message->GetMessage().c_str());
        break;
    case LOGMESSAGE_FOOTER:
        sprintf(buffer, m_logInfo->footerFormat, message->GetMessage().c_str());
        break;
    case LOGMESSAGE_DEBUG:
        sprintf(buffer, "%s %s", m_logInfo->debugPrefix.c_str(), message->GetMessage().c_str());
        break;
    case LOGMESSAGE_ADDRESS:
        sprintf(buffer, "%s %s", m_logInfo->addressPrefix.c_str(), message->GetMessage().c_str());
        break;
    case LOGMESSAGE_VALUE:
        sprintf(buffer, "%s %s", m_logInfo->valuePrefix.c_str(), message->GetMessage().c_str());
        break;
    case LOGMESSAGE_CUSTOM:
    default:
        sprintf(buffer, "test!", message->GetMessage().c_str());
        break;
    }
    try
    {
        if(!m_ofile.is_open() || !m_ofile.good())
            return;

        //string formattedMessage(buffer);
        //formattedMessage.append(m_logInfo->lineTerminator);

        string result;
        if(message->IsUsingTimestamp())
        {
            m_ofile << message->GetTimeStamp().GetTimeString().c_str() << " ";
            //result.append(message->GetTimeStamp().GetTimeString());
            //result.append(" ");
        }

        m_ofile << buffer << m_logInfo->lineTerminator;

        //result.append(formattedMessage);
        //result.push_back('\0');

        //m_ofile.write(result.c_str(), MAX_PATH);
        //m_ofile << result.c_str();
    } 
    catch(std::exception &e)
    {
        MessageBox(NULL, e.what(), "ERROR", NULL);
    }
}

as you can see, I have the call in a try catch block and I even check if the file is valid and open. When I set breakpoints on the call and all around it, the call works fine, but once it reaches the end of the function it gives me this:

Unhandled exception at 0x773515ee in LoggerTest.exe: 0xC0000005: Access violation writing location 0xfeeefeee.

and then it shows the error to occur in this function inside xlock.cpp:

__thiscall _Lockit::_Lockit(int kind)
    : _Locktype(kind)
    {   // lock the mutex
    if (_Locktype < MAX_LOCK)
        _Mtxlock(&mtx[_Locktype]);
    }

My guess is that I have a bad string or pointer somewhere, but I can't pinpoint it.

NOTE: I tried doing

m_ofile << "test!";

and now it gives me assert failure here: _ASSERTE(_CrtIsValidHeapPointer(pUserData));

The .c_str() function returns a pointer, which could cause issues with the ostream output.

Unless there's a reason for having to convert it outside of this block, just pass << result without turning it into ac string and see if that fixes your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM