I am having a problem with MapViewOfFile. Basically this method is called some 55000 times and succeeds over 50000 times, the remainder (approx 5000) returned "The base address or the file offset specified does not have the proper alignment".

The thing is if I repeat the exercise with the previously failed items they all work OK - so I am accessing the same file on disk and trying to create a view of the same part of the file, the first time errors the second time succeed's. Just to be clear the 2nd time is a totally new process, i,e the app has been stopped and restarted. In order to make sure that I pass in the correct offset I call GetSystemInfo to ensure that I am using a multiple of the allocation granularity.

The only difference between the first and second runs would be the the number of times the MappingFile would have been used to supply a view. I am making sure that UnmapViewOfFile is called.

Ultimately several process will be accessing the MappingFile but currently there is only a single process.

The underlying file is opened in READ_ONLY mode.

The call to MapViewOfFile is as follows

pBytes = reinterpret_cast<BYTE*>(MapViewOfFile(m_mapMemMapHandles[m_bstrCurrentName], FILE_MAP_READ, 0, start, bytesToGrab));

if (pBytes == NULL)
{
hr = HRESULT_FROM_WIN32 (GetLastError());

where 'start' is either 0 or a multiple of the SYSTEM_INFO.dwAllocationGranularity. The max size of the file on disk is 100M hence the 3rd param being 0.

So far I have not been able to test to see if this happens again with a different batch of files.

So any ideas on why, for 5000 items, the call fails but succeeds the 2nd time? Any suggestions appreciated as I am being pressured for answers.