Ofstream::write()未处理异常
ofstream::write() unhandled exception
所以我正在编写一个日志库,用于记录各种各样的事情,当我对它运行测试时,它一直崩溃。当我将日志消息写入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(' ');
//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:访问违规写入位置0xfeeeee .
,然后显示在xlock.cpp中的这个函数中发生的错误:
__thiscall _Lockit::_Lockit(int kind)
: _Locktype(kind)
{ // lock the mutex
if (_Locktype < MAX_LOCK)
_Mtxlock(&mtx[_Locktype]);
}
我的猜测是我有一个坏的字符串或指针,但我不能确定它。
注意:我试着做
m_ofile << "test!";
现在它在这里给我assert失败:_ASSERTE(_CrtIsValidHeapPointer(pUserData));
.c_str()函数返回一个指针,这可能导致ostream输出出现问题。
除非有必要在代码块之外转换它,否则只需传递<<结果,而不将其转换为c字符串,看看是否解决了您的问题。
相关文章:
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- C++ 程序中双链表的未处理异常
- 运算符重载时未处理<<异常
- 实验室中0x6BE20E11 (ucrtbased.dll) 的未处理异常.exe: 0xC0000005:访问冲突读取
- ImageMagick 的未处理异常
- 类数组大小为的未处理异常
- 尝试将构造函数设置为 0 - c++ 的未处理异常
- Dijkstra 中0x001e1078 s_Algorithm.exe的未处理异常:0xC0000005:访问冲突读取位置0xbaadf00d
- 运算符重载未处理异常
- 尝试使用 fread 读取文本文件时发生 'System.AccessViolationException' 类型的未处理异常
- c++中的Guard,在main中未处理异常时不调用析构函数
- 某些内存位置的未处理异常
- 错误 linkedlist .exe 中0x00D0345E处的未处理异常: 0xC0000005:访问违规写入位置0x
- 仅在发布模式下使用 glGenBuffer 的未处理异常 - QT
- 如何分析VS2010在调试模式下捕获的未处理异常
- glDrawArrays上的openGL未处理异常(GL_TRIANGLES,0,model.indicesCount)
- 使用Boost Spirit解析语法的未处理异常
- 解析"%Z" C++ 中将错误作为访问 voilation 的未处理异常给出?
- 为什么在win32计时器回调中引发的未处理异常未被调试器视为未处理异常