MemoryMappedViewStream error “does not have appropriate access”

久未见 提交于 2019-12-11 15:43:37

问题


I am trying to create a MMF-backed collection that dynamically expands up to 2GB. I wanted to be able to read existing items at the same time new items are being added. It works great on my development machine but I am getting an error on some machines:

The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

Has anyone seen this error before? I am likely doing something wrong with the way I am handling MemoryMappedFiles, I'm creating a 2GB MMF with the DelayAllocatePages flag so it doesn't use it all right away:

public const long ONE_GIGABYTE = 1073741824;
long maxCapacity = 2 * ONE_GIGABYTE;
mmStorage = MemoryMappedFile.CreateNew(mmfName, maxCapacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.DelayAllocatePages, null, HandleInheritability.Inheritable);

Then I write data to it a chunk at a time, using an internal collection to keep track of where each chunk is located.

lock ( writeLock ) {
    // get the most recently added item
    var lastItem = _itemLocations[_itemLocations.Count - 1];
    // calculate next items offset
    long newOffset = lastItem.Offset + lastItem.Length;

    // add next items data
    using ( var mmStream = mmStorage.CreateViewStream(newOffset, itemBytes.Length, MemoryMappedFileAccess.ReadWrite) ) {
        Trace.WriteLine(string.Format("Writing {0} bytes at location {1}", itemBytes.Length, newOffset));
        mmStream.Write(itemBytes, 0, itemBytes.Length);
    }

    // add location info to list
    _itemLocations.Add(new ItemLocation()
    {
        Offset = newOffset,
        Length = itemBytes.Length
    });
}

On the remote machine the first write goes ok, but the second write is causing the exception I mentioned which kills the program completely.

Writing 5973 bytes at location 0

Writing 5901 bytes at location 5973

The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

Update

I have tried changing

MemoryMappedFileOptions.DelayAllocatePages

to

MemoryMappedFileOptions.None

and it stops throwing the exception, but it also allocates the full 2GB right away. I'd prefer if I could grow the MMF as needed but I guess it wont work on all machines. I'm not sure why DelayAllocatePages works on some machines and not others.

来源:https://stackoverflow.com/questions/46083451/memorymappedviewstream-error-does-not-have-appropriate-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!