【发布时间】:2013-12-04 11:35:59
【问题描述】:
我有一个为 Linux 编写的小应用程序,现在已移植到 Windows。它是单线程的并使用 boost(如果重要的话)。
应用程序正在尝试使用以下代码写入文本文件:
m_oFile.open(oFileName.c_str());
if(!m_oFile.is_open())
{
cerr << "Unable to open output file: " << oFileName.c_str() << endl;
exit(0);
}
m_oFile << "some text goes here\n";
m_oFile 是该类的成员。
文件创建并成功打开;在上面的最后一行代码中抛出异常。
堆栈跟踪:
msvcr100.dll!_lock_file(_iobuf * pf) Line 236 + 0xa bytes C
App.exe!std::basic_filebuf<char,std::char_traits<char> >::_Lock() Line 310 + 0xf bytes C++
App.exe!std::basic_ostream<char,std::char_traits<char> >::_Sentry_base::_Sentry_base(std::basic_ostream<char,std::char_traits<char> > & _Ostr) Line 93 + 0x30 bytes C++
App.exe!std::basic_ostream<char,std::char_traits<char> >::sentry::sentry(std::basic_ostream<char,std::char_traits<char> > & _Ostr) Line 114 + x3a bytes C++
谢谢!
编辑:
当我将代码生成属性更改为使用多线程调试 Dll (/MDd) 而不是多线程 Dll (/MD) 时,一切运行正常。你对此有什么解释吗?
再次感谢。
【问题讨论】:
-
您的文件是否被其他脚本使用?文件是否具有正确的权限?
-
@rursw1 刚刚看到你的帖子,因为我在使用 Visual Studio 2012 时遇到了同样的问题。对我来说
/MDd没有帮助,但从/Od /Ob0到/Od /Ob1确实有帮助。std::ostream::sentry中的某些东西似乎很奇怪。我将它调试到发现堆栈上的指针损坏的地步,但我承认我找不到根本原因。sentry的一般问题请参阅 here、here 和 here。
标签: c++ windows access-violation ofstream visual-studio-2010