I'm using a memory validation program that is reporting handles left in use at program termination. I've narrowed it down to CStdioFile calls. Seems that any CStdioFile opened in InitInstance or in the main window dialog's OnInitDialog in a Dialog app, leave handles lying around.
This is more of a curiosity than a problem but I'd like to understand it in case it is a problem and I'm interpreting it wrong.
So, I took a small app (InitInstance shown here) and put a bit of code in to test it and, sure enough, I get a handle leak. If I change the CStdioFile to CFile and open the file, it doesn't report a leak.
I put the open in braces to see if having it destruct after use would fix it. I tried specifically closing the file and it doesn't help.
BOOL CMyApp::InitInstance()
{
AfxEnableControlContainer();
{
CStdioFile fFile; // Change to a CFile to get no leaks.
fFile.Open("mytestfile.txt", CFile::modeRead);
}
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
//Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CMyDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
}
else if (nResponse == IDCANCEL)
{
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
I'd like to see no leaks to know the code is clean. And I'm really curious about why CFile and CStdioFile would behave differently.