BUG: "Out of Memory" Error When Saving to an Existing FileLast reviewed: July 2, 1997Article ID: Q163253 |
4.00 4.10 4.20
WINDOWS NT
kbprg kbbuglist kbcode
The information in this article applies to:
SYMPTOMSWhen you save to an existing file in a Visual C++ MFC application, an "Out of memory" error occurs. The error occurs only when you save over an existing file on a non-NT platform, such as Windows 95, that is on a network. For example, if you run the Scribble sample on a Windows NT machine and try to save the scribble file to an existing file on a Windows 95 share, the error occurs.
CAUSEMFC uses a class called CMirrorFile that protects a file in cases where an error may occur when you are writing to a file. MFC first writes out to a temporary file and then copies over to the final file when all writing is done. In the CMirrorFile::Open() function located in Doccore.cpp, the following code exists:
DWORD dwLength = 0; PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL; GetFileSecurity(lpszFileName, DACL_SECURITY_INFORMATION, NULL, dwLength, &dwLength); pSecurityDescriptor = (PSECURITY_DESCRIPTOR) new BYTE[dwLength];In the case where a file is located on a non-NT share, the GetFileSecurity call fails. It may return a large value and this can cause the new statement to fail with an "out of memory" error. You need to select the return value for GetFileSecurity.
RESOLUTIONThere are a couple of ways to work around the problem.
#include <afxpriv.h> #include "c:\msdev\mfc\src\afximpl.h"class CMyMirrorFile: public CMirrorFile { virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);}; BOOL CMyMirrorFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError){ ASSERT(lpszFileName != NULL); m_strMirrorName.Empty();#ifndef _MAC CFileStatus status; if (nOpenFlags & CFile::modeCreate) //opened for writing { if (CFile::GetStatus(lpszFileName, status)) { CString strRoot; AfxGetRoot(lpszFileName, strRoot); DWORD dwSecPerClus, dwBytesPerSec, dwFreeClus, dwTotalClus; int nBytes = 0; if (GetDiskFreeSpace(strRoot, &dwSecPerClus, &dwBytesPerSec,&dwFreeClus, &dwTotalClus)) { nBytes = dwFreeClus*dwSecPerClus*dwBytesPerSec; } if (nBytes > 2*status.m_size) // at least 2x free space avail { // get the directory for the file TCHAR szPath[_MAX_PATH]; LPTSTR lpszName; GetFullPathName(lpszFileName, _MAX_PATH, szPath, &lpszName); *lpszName = NULL; //let's create a temporary file name GetTempFileName(szPath, _T("MFC"), 0, m_strMirrorName.GetBuffer(_MAX_PATH+1)); m_strMirrorName.ReleaseBuffer(); } } }#endif
if (!m_strMirrorName.IsEmpty() && CFile::Open(m_strMirrorName, nOpenFlags, pError)) { m_strFileName = lpszFileName;#ifndef _MAC FILETIME ftCreate, ftAccess, ftModify; if (::GetFileTime((HANDLE)m_hFile, &ftCreate, &ftAccess, &ftModify)) { AfxTimeToFileTime(status.m_ctime, &ftCreate); SetFileTime((HANDLE)m_hFile, &ftCreate, &ftAccess, &ftModify); } // If GetFileSecurity returns an error, don't do anything with // file securities. DWORD dwLength = 0; PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL; if (GetFileSecurity(lpszFileName, DACL_SECURITY_INFORMATION, NULL, dwLength, &dwLength)) { pSecurityDescriptor = (PSECURITY_DESCRIPTOR) newBYTE[dwLength]; if (::GetFileSecurity(lpszFileName, DACL_SECURITY_INFORMATION, pSecurityDescriptor, dwLength, &dwLength)) { SetFileSecurity(m_strMirrorName, DACL_SECURITY_INFORMATION,pSecurityDescriptor); } delete[] (BYTE*)pSecurityDescriptor; }#endif return TRUE; } m_strMirrorName.Empty(); return CFile::Open(lpszFileName, nOpenFlags, pError);} ************* Then you need to override CDocument::GetFile() to return an object of your new derived class. Please note that the work around uses the undocumented class CMirrorFile. The work around may have to be modified to work in a future version of MFC. Refer to this article again in the future for any updates and modifications that may be required for future versions.
STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.
|
KBCategory: kbprg kbbuglist kbcode
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |