Consider the following:
std::basic_fstream<char> testfile;
testfile.write(reinterpret_cast<const char*>(&someInt), sizeof(int));
testfile.close();
This runs with no complaint when built with VC 8.0, but crashes when built with VC 10.0 beta.
I have some legacy code that actually relies on the VC 8 behavior, where we inherit from basic_fstream to add functionality:
class myFile : public basic_fstream<char> {
public:
void myWrite(const char* data, std::streamsize len) {
write(data, len);
// update some state variables (checksum, etc)
}
};
There are cases where it is beneficial to inspect the additional state without incurring the disk I/O (e.g. test writes).
I'm assuming this is undefined behavior, and I'm lucky it doesn't crash in VC 8. That said, I've had enough issues evaluating VS 2010 beta that I'd like to be sure. Can anyone out there say definitively?
EDIT: Call stack in VS 2010:
ostream::write
ostream::sentry ctor
istream::_Sentry_base ctor
fstream::_Lock
_file.c::_lock_file
crashes on EnterCriticalSection( &(((_FILEX *)pf)->lock) ), pf is null
Call stack on VS 2005:
ostream::write
ostream::sentry ctor
ostream::_Sentry_base ctor // different
streambuf::_Lock
_Mutex::_Lock()
_Mtxlock in xmtx.c
EnterCriticalSection(_Mtx), where _Mtx is valid
Also, compiles and runs with no errors with gcc-4.3.3 on Ubuntu.
*** EDIT:
After more digging, it appears that this in fact is a bug in Visual Studio 2010 Beta 1.
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=456890
According to this report, it has been fixed for the official release.
Thanks for all of your input.