CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2014
    Posts
    3

    Intermittent error with MapViewOfFile

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Intermittent error with MapViewOfFile

    When using MapViewOfFile(), the offset into the file must be a multiple of the system's allocation granularity (usually 64kb). Use GetSystemInfo() to determine the allocation granularity. (See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx and http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx)
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2014
    Posts
    3

    Re: Intermittent error with MapViewOfFile

    Quote Originally Posted by 2kaud View Post
    When using MapViewOfFile(), the offset into the file must be a multiple of the system's allocation granularity (usually 64kb). Use GetSystemInfo() to determine the allocation granularity. (See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx and http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx)
    Thanks for the reply but I did explain that I am already doing this.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Intermittent error with MapViewOfFile

    You are for sure not the first who has such (or similar) problem. Try to look at the
    https://www.google.com/search?q=MapV...F-8&gws_rd=ssl
    Perhaps, you could find the explanation/solution?
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2014
    Posts
    3

    Re: Intermittent error with MapViewOfFile

    Quote Originally Posted by VictorN View Post
    You are for sure not the first who has such (or similar) problem. Try to look at the
    https://www.google.com/search?q=MapV...F-8&gws_rd=ssl
    Perhaps, you could find the explanation/solution?
    Thanks I will check out

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured