Here's my code, this is a trivial test program I wrote to isolate the problem.
Code:
#include <fstream>
#include <string>
using namespace std;
void main()
{
fstream file;
char filename[255];
strcpy(filename,"C:\\temp\\test.txt");
file.open(filename, fstream::out);
}
When I run it with debugging it throws an exception in open.c, in the function _tsopen_nolock(...), at the line
Code:
if ( (osfh = CreateFile( (LPTSTR)path,
fileaccess,
fileshare,
&SecurityAttributes,
filecreate,
fileattrib,
NULL ))
== (HANDLE)(-1) )
The exception message is:
Unhandled exception at 0x7721631f in TestProject.exe: 0xC0000008: An invalid handle was specified.
Note that this is NOT just a "first-chance" exception from the debugger that I can ignore, even if I continue I get the same exception again, without the "first-chance" qualifier, and the file is not successfully opened and cannot be written to.
After some digging around with WinDbg and searching online I found that the cause of this exception is:
Invalid Handle exception (0xC0000008) can frequently be seen in crash dumps. It results from an invalid handle value passed to CloseHandle and other Win32 API or when a handle or return status is checked manually for validity and the same exception is raised via RaiseException or internally via RtlRaiseStatus.
... but that doesn't help me very much.
Once the exception it thrown if I go to the specified location then the file HAS been created, and Windows tells me I can't delete it because "the file is open in TestProject.exe".
Finally, if I run the program using Ctrl-F5, start program without debugging, then it runs perfectly and I can write to the file.
This problem seems to just have come out of nowhere. I can't think of anything that I changed between last Friday when it was working and yesterday when it this started happening. Any ideas GREATLY appreciated.
<EDIT> I forgot to mention I'm using MS Visual C++ 2010 Express </EDIT>