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

发布于 2024-12-21 00:23:07 字数 2592 浏览 1 评论 0原文

因此,我正在编写一个日志库来记录各种事情,当我对其进行测试时,它不断崩溃。当我将日志消息写入 ofstream 文件时,我将异常范围缩小到了写入函数。我解析消息和内容,然后实际调用 ofstream::write()。这是我收到重新运行错误的部分:

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

如您所见,我在 try catch 块中进行了调用,甚至检查文件是否有效并打开。当我在调用及其周围设置断点时,调用工作正常,但是一旦到达函数末尾,它就会给出以下内容:

LoggerTest.exe 中 0x773515ee 处未处理的异常:0xC0000005:写入位置 0xfeeefeee 时出现访问冲突。

然后它显示 xlock.cpp 内的此函数中发生的错误:

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

我的猜测是我在某处有一个错误的字符串或指针,但我无法查明它。

注意:我尝试这样做

m_ofile << "test!";

,现在它给我断言失败:_ASSERTE(_CrtIsValidHeapPointer(pUserData));

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2024-12-28 00:23:07

.c_str() 函数返回一个指针,这可能会导致 ostream 输出出现问题。

除非有必要将其转换到该块之外,否则只需传递 <<结果而不将其转换为交流字符串,看看是否可以解决您的问题。

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 a c string and see if that fixes your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文