Error introduced in MemoryMappedFile::openInternal

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

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.

1 Like

Ouch, good spot. Thanks Dave!

This is on develop now:

1 Like