简体   繁体   English

ofstream :: write()未处理的异常

[英]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. 当我将日志消息写到ofstream文件时,我将异常范围缩小到写函数。 I parse the message and stuff, and then I have the actual call to ofstream::write(). 我解析消息和内容,然后实际调用了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. 如您所见,我将调用保存在try catch块中,甚至检查文件是否有效并打开。 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. LoggerTest.exe中0x773515ee处未处理的异常:0xC0000005:访问冲突写入位置0xfeeefeee。

and then it shows the error to occur in this function inside xlock.cpp: 然后显示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)); 现在它在这里使我断言失败: _ASSERTE(_CrtIsValidHeapPointer(pUserData));

The .c_str() function returns a pointer, which could cause issues with the ostream output. .c_str()函数返回一个指针,这可能导致ostream输出出现问题。

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. 除非有必要将其转换为该块之外的原因,否则只需传递<<结果而不将其转换为ac字符串,看看是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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