I have a program that reads a set of files, closes them, and then attempt to delete them.
Sometimes (not always, but pretty often) the delete fails with 'sharing violation' error.
Using sysinternals process monitor I saw that in these cases the close operation wasn't reflected in the process monitor.
It appears that sometimes the close system call is skipped for no apparent reason, and without any exception.
This is happening on a windows 7 64bit machine using visual studio 2010.
Code sample;
void readFile(string file)
{
ifstream stream(file);
string line;
while(getline(stream, line))
{
cout << line << endl:
}
stream.close(); // this is redundant
}
// calling code:
readFile(file);
if(remove(file.c_str()) != 0)
{
cout << "file deletion failed" << endl;
}