2

I have a small application that was written for Linux and now was ported to Windows. It's single-threaded and uses boost (If it matters).
The application is trying to write to a text file using the following code:

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 is a member of the class.
The file is created and opened successfully; the exception is thrown at the last code line above.

Stack trace:

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++

Thanks!

EDIT:

When I changed the Code Generation properties to use Multi Threaded Debug Dll (/MDd) instead of Multi Threaded Dll (/MD) everything runs properly. Do you have any explanation for that?
Thanks again.

rkellerm
  • 5,362
  • 8
  • 58
  • 95
  • Is your file used by another script? Has the file the right permissions? – Luca Davanzo Dec 04 '13 at 11:38
  • @rursw1 Just came across your post, because I had the same problem with Visual Studio 2012. For me `/MDd` didn't help, but going from `/Od /Ob0` to `/Od /Ob1` did help. It seems something in [`std::ostream::sentry`](http://www.cplusplus.com/reference/ostream/ostream/sentry/) is weird. I debugged it to a point where I found a pointer on the stack broken, but I admit I couldn't find the root cause. General problems with `sentry` see [here](http://stackoverflow.com/questions/1140714), [here](http://stackoverflow.com/questions/4340396) and [here](https://root.cern.ch/phpBB3/viewtopic.php?t=8164). – Florian Oct 23 '15 at 14:28

2 Answers2

1

Oh - the msvc incompatibility of libraries nightmare - yep make sure all(!) libraries match each other.

  • no mixing debug an release
  • not mixing single and multi-threaded
  • ...
0

Take a look here and try to open your file in read/write mode.

m_oFile.open(oFileName.c_str(), ios::out | ios::in);

Otherwise, check file's permissions.

edit

Remember to always close your file at the end.

m_oFile.close();

Another hit: could the file be used from another thread?

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
  • Thank you, but read / write mode wasn't the issue. Permissions are ok, as the file is created successfully. – rkellerm Dec 04 '13 at 11:56