# Error introduced in MemoryMappedFile::openInternal

**URL:** <https://forum.juce.com/t/error-introduced-in-memorymappedfile-openinternal/47291>\
**Category:** General JUCE discussion\
**Created:** [August 11, 2021, 10:28am UTC](https://forum.juce.com/t/error-introduced-in-memorymappedfile-openinternal/47291 "2021-08-11T10:28:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [August 11, 2021, 10:28am UTC](https://forum.juce.com/t/error-introduced-in-memorymappedfile-openinternal/47291/1 "2021-08-11T10:28:22Z")

</div>

I’ve just spent a very long time tracking down a particularly tricky bug introduced in this commit: [POSIX: Close file handle after mapping in MemoryMappedFile::openInte… · juce-framework/JUCE@0f76c7d · GitHub](https://github.com/juce-framework/JUCE/commit/0f76c7d09771e2e392cb8581561ce6a858b51635)

The error manifested in a set of tests that writes a lot of audio files and reads back from them. After a certain number of tests the write suddenly would fail with a “Bad file descriptor” status.

After a lot of manual bisecting I think what’s happening is elsewhere I’m:

- Creating a memory mapped file to read
- This now closes the handle after it’s mapped but doesn’t set the handle to 0
- Elsewhere I open a new file for writing which gets the same descriptor as the one previously used
- When the MemoryMappedFile goes out of scope it closes the descriptor again because it’s non-zero
- This then ruins any operations on that seemingly unrelated file

I think the fix is simply to set `fileHandle = 0;` on line 581.

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [August 11, 2021, 12:48pm UTC](https://forum.juce.com/t/error-introduced-in-memorymappedfile-openinternal/47291/2 "2021-08-11T12:48:27Z")

</div>

Ouch, good spot. Thanks Dave!

This is on `develop` now:

> <https://github.com/juce-framework/JUCE/commit/055a4e93e9ed1023af970fa05e5b2b4e5d087685>
