# Crash when reading some Midi Files

**URL:** <https://forum.juce.com/t/crash-when-reading-some-midi-files/33617>\
**Category:** General JUCE discussion\
**Created:** [May 28, 2019, 1:58pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617 "2019-05-28T13:58:42Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [May 28, 2019, 1:58pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/1 "2019-05-28T13:58:42Z")

</div>

Hello,

When trying to read some Midi files, Juce crashes.  
[3am03.mid.cpp](https://forum.juce.com/uploads/short-url/tFyLFH1GuZCAivbZMrk2qpO3fXu.cpp) (40.6 KB)

I attach one of the involved Midi files. (changed the extension that it uploads)

It sometimes raises two or three of the below exceptions. The two jassert are raised in juce\_MidiMessage.cpp, and the third one in juce\_ByteOrder.h. When not raised, I got some midi messages (never the same number) with negative timestamps, as if not initialized.  
I tried with and without a pointer for the FileInputStream, for the same result.  
Also, the code works fine with plenty of other midi files.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/c/cdbff00f5e0fec68643b10cb88e348af34e6339f.png) ![image|658x235] ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/0/02e636cb2d04f64037652e89d0e1905c2283675a.png) (upload://ko05AkgVhHjjK5ch9CZD4QagaYp.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/8/832d14f217665527ae12c214b2f466346c43104d.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/7/7f066faa7ac9240c2fbb18f7c53bbb6d784407a7.png)

This file opens fine with a DAW (tested with S1 and reaper)

Any idea what to fix here?

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [May 30, 2019, 7:19pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/2 "2019-05-30T19:19:27Z")

</div>

Hello,  
No answer on that one from the Juce team? ☹  
Don’t know how I could be more accurate in the issue, provided the 2 lines of code and the Midi file which has the issue.

Thanks

---

<div class="post-metadata">

**Author:** ![jsolo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jsolo/32/30_2.png) [@jsolo](https://forum.juce.com/u/jsolo)\
**Post date:** [May 30, 2019, 8:57pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/3 "2019-05-30T20:57:42Z")

</div>

@Rachmaninov the following code works for me here. `maOS 10.14.5`.

```auto
juce::FileInputStream stream (juce::File ("/path/to/file/3am03.mid"));
juce::MidiFile midiFile{};
if (midiFile.readFrom (stream) && midiFile.getNumTracks() > 0) {
    midiFile.convertTimestampTicksToSeconds();
    
    auto numTracks = midiFile.getNumTracks();
    for (int i = 0; i < numTracks; ++i) {
        auto midiTrack = midiFile.getTrack (i);
        for (auto e : *midiTrack) {
            DBG (e->message.getDescription());
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [June 4, 2019, 9:19am UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/4 "2019-06-04T09:19:21Z")

</div>

Hello hhit,  
Your code gives us the same error (Windows10). ☹

---

<div class="post-metadata">

**Author:** ![oxxyyd](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oxxyyd](https://forum.juce.com/u/oxxyyd)\
**Post date:** [June 7, 2019, 10:35pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/5 "2019-06-07T22:35:40Z")

</div>

I get similar errors, beginning with track 8. Might be that this track contains some corrupted data that Juce can’t handle gracefully, i.e. show an error or just skip instead of crashing.

What’s the origin of the file? In the daws you’ve tested, what does the midi content look like?

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [June 11, 2019, 2:30pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/6 "2019-06-11T14:30:27Z")

</div>

The content looks normal to me in a DAW but I guess there is something wrong in these files. However, of course, it should not crash !

---

<div class="post-metadata">

**Author:** ![tomto66](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tomto66/32/4916_2.png) [@tomto66](https://forum.juce.com/u/tomto66)\
**Post date:** [June 11, 2019, 4:36pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/7 "2019-06-11T16:36:05Z")

</div>

FWIW I made a couple of plugins that read Midi files - when I try to read your they also crash. I’ll see if I can debug this (but that’s not high on my todo list).

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [June 12, 2019, 7:57am UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/8 "2019-06-12T07:57:19Z")

</div>

Maybe it’s Roli’s job to fix their bugs? 🙄

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [June 12, 2019, 9:00am UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/9 "2019-06-12T09:00:34Z")

</div>

There’s something wrong with the MIDI file… [MIDIKit](https://www.mixagesoftware.com) fails to load it:

6/12/19 1:55:39 AM ~/Desktop/3am03.mid [ERROR# -39 while opening]

Rail

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [June 12, 2019, 10:45am UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/10 "2019-06-12T10:45:49Z")

</div>

Yes, but it should not crash.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 12, 2019, 10:49am UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/11 "2019-06-12T10:49:57Z")

</div>

The code you’re pointing out is NOT a crash. It’s an assertion.

Does it actually crash if you continue (i.e. what would happen in a release buid)?

Or does it just carry on and load as much as it can from this obviously broken MIDI file, just like any other host would?

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [June 12, 2019, 12:37pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/12 "2019-06-12T12:37:27Z")

</div>

Hello Jules,

Of course it crashes in a release build, otherwise I would not have opened the thread.

---

<div class="post-metadata">

**Author:** ![tomto66](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tomto66/32/4916_2.png) [@tomto66](https://forum.juce.com/u/tomto66)\
**Post date:** [June 12, 2019, 12:46pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/13 "2019-06-12T12:46:49Z")

</div>

Frankly I have not run this in a debugger yet - I’ll try and do that tonight. I overlooked the assertion - that points to a corrupted file really - a negative note number or one above 128.

I have loaded tons of Midi files using Juce and never had a crash, so the comment like Roli should fix their bugs seems out of line.

---

<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:** [June 12, 2019, 1:55pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/14 "2019-06-12T13:55:54Z")

</div>

One of the track length chunks in the file is incorrect which was causing JUCE’s midi file parsing to fall over. I’ll push a fix shortly.

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [June 12, 2019, 1:59pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/15 "2019-06-12T13:59:46Z")

</div>

Thank you 😉

---

<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:** [June 12, 2019, 2:47pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/16 "2019-06-12T14:47:47Z")

</div>

> <https://github.com/WeAreROLI/JUCE/commit/0ed474e412c7fa3647756b7169a1428e2778c7a5>

---

<div class="post-metadata">

**Author:** ![Rachmaninov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rachmaninov/32/5074_2.png) [@Rachmaninov](https://forum.juce.com/u/Rachmaninov)\
**Post date:** [October 12, 2020, 2:03pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/17 "2020-10-12T14:03:22Z")

</div>

Hello there,  
Found 3 new files that make Juce crash 😉  
[ba\_bran3.mid.cpp](https://forum.juce.com/uploads/short-url/uL3EA84iTbIKInWp0xRlr54DIHv.cpp) (122.7 KB) [dixie-nw.mid.cpp](https://forum.juce.com/uploads/short-url/jGudqPazbbmam4NUwQnLPsplXCM.cpp) (12.5 KB) [Impersong.mid.cpp](https://forum.juce.com/uploads/short-url/4JobG97YEeQdksgS8THqC3uIasR.cpp) (17.0 KB)

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 23, 2020, 3:42pm UTC](https://forum.juce.com/t/crash-when-reading-some-midi-files/33617/18 "2020-10-23T15:42:32Z")

</div>

Thanks for raising this issue and providing some useful test cases. I’ve pushed some fixes here:

> <https://github.com/juce-framework/JUCE/commit/094329199037cccc2535ad1a7327b10bedf24afd>

Just to avoid any ambiguity, it’s probably worth pointing out that all of those new files are malformed. A couple have incorrect track lengths, and one has an incorrect header length. Therefore, they will still fail to load, although they will no longer cause a crash.
