CFile::Close does not flush unwritten data before close the handle. Why?

  • Thread starter Thread starter tempc
  • Start date Start date
T

tempc

Guest
Hi,

Originally I think CFile::Close will flush all unwritten data into the disk, then close the file handle gracefully. However, after checking the source code for CFile::Close, I find the code below:

void CFile::Close()
{
ASSERT_VALID(this);
ASSERT(m_hFile != INVALID_HANDLE_VALUE);
BOOL bError = FALSE;
if (m_hFile != INVALID_HANDLE_VALUE)
bError = !::CloseHandle(m_hFile);

m_hFile = INVALID_HANDLE_VALUE;
m_bCloseOnDelete = FALSE;
m_strFileName.Empty();

if (bError)
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
}
So it does not invoke CFile::Flush to flush the buffer at all. The only chance is ::CloseHandle. But in document for ::CloseHandle at CloseHandle function (handleapi.h) , it does not mention that the function will flush the buffer as well.

Therefore, if some data are in the written buffer, then CFile::Close will just ignore them and close the file, which makes data losses. Also such a behavior is not expected. Very strange.

Continue reading...
 
Back
Top