BUG: Creating File Mapping Objects Using the Same File Handle Will Cause a File to Be Inaccessible

ID: Q230606


The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API), included with:
    • Microsoft Windows versions 95, 98


SYMPTOMS

Creating more than one file mapping object using the same file handle in a process may cause the corresponding named file on the disk to be inaccessible. Even though an application closes the file handle as well as the file mapping handles, the file on the disk cannot be deleted or renamed or replaced thereafter. When this problem occurs, Windows must be restarted to get access to this file on the disk.


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

The CreateFileMapping() Win32 API takes a handle to a file from which a file mapping object is created. An application can create a file mapping object backed by a named file in the file system using a valid file handle. If the application creates more than one file mapping object using the same file handle, the corresponding file on the disk will be inaccessible thereafter. The following code reproduces this problem:


    HANDLE hFile;
    HANDLE hMapping1 = NULL;
    HANDLE hMapping2 = NULL;

    hFile = CreateFile ("test.dat",
                GENERIC_READ | GENERIC_WRITE,
                0,
                NULL,
                OPEN_ALWAYS,
                FILE_ATTRIBUTE_NORMAL,
                NULL);

    if (hFile != INVALID_HANDLE_VALUE)
    {
        hMapping1 = CreateFileMapping (hFile, NULL,
                        PAGE_READWRITE, 0, 0, NULL);
        hMapping2 = CreateFileMapping (hFile, NULL,
                        PAGE_READWRITE, 0, 0, NULL);
        if (hMapping1)
            CloseHandle (hMapping1);
        if (hMapping2)
            CloseHandle (hMapping2);
        CloseHandle (hFile);
    } 
This problem occurs only if an application creates another file mapping object using the same file handle and the second file mapping object is created before closing the existing file mapping handle.

An application can either work around this by closing the existing file mapping handle before creating another file mapping object or specify a different file handle for each file mapping object. However, if a different file handle is required, the file on the disk needs to be opened for shared access.

Additional query words:

Keywords : kbAPI kbKernBase kbMemory kbSDKPlatform kbSDKWin32
Version : winnt:
Platform : winnt
Issue type : kbbug


Last Reviewed: October 15, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.