0

Good morning, I am trying to write a cMemoryMappedFile class that deals with large files that cannot be mapped to one view in Win32. The code is at the pastebin url.

In GetPointer(), we are trying to carefully map and unmap file regions as they are needed. We are encountering an access violation in GetPointer() on the line marked with the comment // crashes here if adjusted ptr is bad ptr.

The test program is:

int TotalKeys = 2709783;
try {
    mmapFile = new cMemoryMappedFile(TotalKeys * 54 + 1);   
}
catch (cException e) {
    printf("catch cMemoryMappedFile\n");
    throwl     
}

for (i = 0; i < 50000; i++) {
    mmapFile->GetPointer(i * 54);
}

When i equals 28521 and i * 54 = 1540134, we get an access violation reading location 0x0024000. The MEM_BLOCK_SIZE is on the 64K alignment required by MapViewOfFile, MEM_BLOCK_SIZE = 65536 * 2. We are running Windows XP Professional Version 2002 Service Pack 3, 32 bit version. What is puzzling is that we are trying the move the map view within the file mapping. Please suggest the cause of the access violation if you wish.

Thank you.

8
  • Please use the {} button to fix your formatting...
    – bdonlan
    Mar 1, 2011 at 15:16
  • @bdonlan, Could you please tell me where the {} button is so I can fix my formatting? Thank you.
    – Frank
    Mar 1, 2011 at 15:20
  • 1
    I think 300+ lines of code is too much; try to reduce the amount of code to only that which is necessary or use something like pastebin.com.
    – Luke
    Mar 1, 2011 at 15:29
  • 1
    Have you tried reading this? Me neither!
    – Steve
    Mar 1, 2011 at 15:32
  • 1
    @Luke, I apologize for not using www.pastebin.com. I don't know if you can paste pastebin.com urls in StackOverflow posts. Here is the pastebin.url for source code and header file: <iframe src="pastebin.com/embed_iframe.php?i=teuGB43x" style="border:none;width:100%"></iframe>
    – Frank
    Mar 1, 2011 at 15:50

2 Answers 2

2

This calculation seems overly complicated.

char * cMemoryMappedFile::GetPointer(int n){
    unsigned int baseoff;
    if( n < MEM_BLOCK_SIZE / 2)
    {
       baseoff = 0;
    }
    else
    {
        baseoff = ((n + MEM_BLOCK_SIZE / 4) &
           (~(MEM_BLOCK_SIZE / 2 - 1))) - MEM_BLOCK_SIZE / 2;
    }
    ...

As I understand, n is the desired offset into the file, and baseoff is the first address we'll actually map. We want the largest baseoff <= n, where baseoff is a multiple of MEM_BLOCK_SIZE. So how about:

    unsigned int baseoff = (n / MEM_BLOCK_SIZE) * MEM_BLOCK_SIZE;

Later:

    MapPtr = (char*)::MapViewOfFile(hMapping, FILE_MAP_WRITE | FILE_MAP_READ, 0,
                    baseoff, mappedlength);

You should check the result of MapViewOfFile.

It's not clear what you want the return value to be. I'm assuming adjustedptr + n should point to the originally requested byte. But then you try to access *adjustedptr, which is not guaranteed to be part of the view when the base offset is larger than MEM_BLOCK_SIZE. I think you want the printf to use *(adjustedptr + n) or adjustedptr[n]. Or maybe you're trying to access the first byte actually mapped, which would be adjustedptr[baseoff].

3
  • Thank you for your reply. I will try your suggestions as soon as possible and I will let you know the result. It is an excellent analysis. If I could you 5 votes, I would, Thank you
    – Frank
    Mar 1, 2011 at 18:50
  • I tested your suggestions. When n = 65610, the Microsoft operator delete[] returns a DEBUG_ASSERTION_FAILED _CrtIsValidHeapPointer(pUserData). Here is the www.pastebin url for the code snippet showing where Microsoft complains about the operaror delete. <iframe src="pastebin.com/embed_iframe.php?i=mvmxLa62" style="border:none;width:100%"></iframe>. I used valgrind on Centos Unix a week ago to test for heap corruption in this code snippet and valgrind didn't report any heap errors. Maybe this problem is unique to Microsoft.
    – Frank
    Mar 1, 2011 at 20:40
  • I just accepted your answer. Could you think of a workaround to this Microsoft operator delete [] probem? Thank you for your help.
    – Frank
    Mar 1, 2011 at 20:47
0
 I fixed the access violation and the Microsoft operator delete error. The modified code is in the www.pastebin.com url.<iframe src="http://pastebin.com/embed_iframe.php?i=4dsVfkzg" style="border:none;width:100%"></iframe> > I wish to thank Adrian 

McCarthy and Andre Caron for their help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.