Search code examples
c++windows-mobilememory-mapped-files

MapViewOfFile freezes the Windows Mobile 6 device


I have a Visual Studio 2008 C++ project for Windows Mobile 6 ARMV4I where I'm using memory mapped files. Unfortunately, it causes the device to lock up. I can demonstrate the issue with this code:

#include <list>
#include <algorithm>

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD alloc_size = 256;
    DWORD alloc_max = 16 * 1024 * 1024;
    DWORD alloc_count = alloc_max / alloc_size;

    HANDLE f = ::CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, alloc_max, NULL );

    std::list< void* > l;
    for( DWORD i = 0; i < alloc_count; ++i )
    {
        // device freezes after 65529 iterations
        l.push_back( ::MapViewOfFile( f, FILE_MAP_READ | FILE_MAP_WRITE, 0, i * alloc_size, alloc_size ) );
    }

    std::for_each( l.rbegin(), l.rend(), ::UnmapViewOfFile );
    ::CloseHandle( f );
    return 0;
}

The Windows Mobile 6 Classic Emulator will freeze after 65529 iterations in my testing. Is this something I'm doing incorrectly or is there a platform issue I should be aware of?

Thanks, PaulH

Edit: Increasing to /STACK:1048576,4096 allows me to hit 65535 iterations before the device freezes.

Edit2: According to GlobalMemoryStatus just before the failure, the device has 70.5MB / 94.1MB free physical memory.

Edit3: I can create two MMFs and load them both up to 65500 * 256 bytes. But, neither of them can individually exceed 65535 allocations. Actually, the alloc size doesn't matter. I can cut it in half to 128 bytes each, but I still fail in >65535 iterations.

Edit4: Backing the MMF with an actual file seems to make no difference. Failure in >65535 iterations.


Solution

  • I spoke with somebody that has access to the sources. As it turns out, MapViewOfFile uses an internal reference counter that is a USHORT. So, on the 65535th iteration, it overflowed and caused hate and discontent all over the place eventually halting the system. So, there is an undocumented limit of 65535 open views in to a Memory Mapped File.

    -PaulH