Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7816879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:02:05+00:00 2026-06-02T06:02:05+00:00

I have code which creates a memory mapped file as follows: using (Mutex mutex

  • 0

I have code which creates a memory mapped file as follows:

using (Mutex mutex = new Mutex(false, CoordinatorMutexName))
{
    mutex.WaitOne();

    var security = new MemoryMappedFileSecurity();
        security.SetAccessRule(
            new AccessRule<MemoryMappedFileRights>(
                new SecurityIdentifier(WellKnownSidType.WorldSid, null), // everyone
                MemoryMappedFileRights.FullControl,
                AccessControlType.Allow));

    MemoryMappedFile coordinator = MemoryMappedFile.CreateOrOpen(
        CoordinatorMMFName,
        16 * 1024 * 1024 + 4,
        MemoryMappedFileAccess.ReadWrite,
        MemoryMappedFileOptions.DelayAllocatePages,
        security,
        HandleInheritability.None);

...
...

}

Under certain circumstances (described below), the CreateOrOpen call throws the following exception:

 System.IO.IOException: The handle is invalid.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpenCore(SafeFileHandle fileHandle, String mapName, HandleInheritability inheritability, MemoryMappedFileSecurity memoryMappedFileSecurity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, Int64 capacity)
   at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen(String mapName, Int64 capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability)

It only throws this exception while running automated tests, I can’t reproduce this locally, inside or outside a debugger. I tried extracting just the above code into stand-alone tests, and could not reproduce the issue. But there’s too much code to post everything here. The MemoryMappedFile is persisted for the duration of the thread (assuming it gets created), and then disposed.

Here’s the conditions where the tests fail: The tests are designed to exercise this code from multiple threads. They’re in NUnit, running under CruiseControl.NET, whose service is running under a domain account (not a local machine account), on 64 bit Windows 2008 Server. I can run those same tests manually while logged into the same machine, and they all pass.

I know that’s probably not enough information for someone to solve directly, but I’m at a loss of even how to investigate this problem. What kinds of things could cause a “handle is invalid” message while trying to CreateOrOpen a memory mapped file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:02:10+00:00Added an answer on June 2, 2026 at 6:02 am

    It is documented error code for CreateFileMapping(), the underlying winapi function that creates a memory mapped file:

    If lpName matches the name of an existing event, semaphore, mutex, waitable timer, or job object, the function fails, and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same namespace.

    So pick a good random name to avoid this error. You can get one from Visual Studio: Tools + Create GUID, option 4. It is Globally Unique.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which creates a new table. var html = '<table>';
I have the following code which creates an ImageButton and plays a sound when
I have created some python code which creates an object in a loop, and
I have this code, which creates an image, and then adds some effects to
I have this code which should create a splash image with either no animation
I have the following code which is created with a datalist control: http://jsfiddle.net/vmE2E/1/ But
I have some code which I did not originally create that uses _beginthreadex and
I have created a piece of code which takes an IP address (from main
In my project, I have created a code snippet which can be copied and
I have this code that lets me create a custom UIBarButton which is inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.