# Plugin with MidiOutput crashing on exit

**URL:** <https://forum.juce.com/t/plugin-with-midioutput-crashing-on-exit/9976>\
**Category:** Audio Plugins\
**Created:** [February 14, 2013, 9:58pm UTC](https://forum.juce.com/t/plugin-with-midioutput-crashing-on-exit/9976 "2013-02-14T21:58:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikreiman](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nikreiman/32/273_2.png) [@nikreiman](https://forum.juce.com/u/nikreiman)\
**Post date:** [February 14, 2013, 9:58pm UTC](https://forum.juce.com/t/plugin-with-midioutput-crashing-on-exit/9976/1 "2013-02-14T21:58:09Z")

</div>

I’m writing a VST plugin which creates a virtual MIDI output, the end goal of which is to send things like MIDI clock which normally a VST host would disregard. Anyways, the actual MIDI stuff seems to be working fine, but whenever I remove the plugin I get a crash on destruction. The backtrace looks something like this for the plugin’s thread:

> [@](#):
>
> Thread 0:: Dispatch queue: com.apple.main-thread  
> 0 libsystem\_kernel.dylib 0x9092bc72 \_\_semwait\_signal + 10  
> 1 libsystem\_c.dylib 0x93348a61 nanosleep$UNIX2003 + 189  
> 2 com.teragonaudio.BeatCounter 0x17065340 juce::thread::sleep(int) + 100 (juce\_posix\_SharedCode.h:194)  
> 3 com.teragonaudio.BeatCounter 0x17065510 juce::thread::waitForThreadToExit(int) const + 298 (juce\_Thread.cpp:161)  
> 4 com.teragonaudio.BeatCounter 0x1707a7e7 juce::thread::stopThread(int) + 309 (juce\_Thread.cpp:186)  
> 5 com.teragonaudio.BeatCounter 0x1707af63 juce::thread::~Thread() + 249 (juce\_Thread.cpp:47)  
> 6 com.teragonaudio.BeatCounter 0x16f77217 juce::MidiOutput::~MidiOutput() + 197 (juce\_mac\_CoreMidi.cpp:353)  
> 7 com.teragonaudio.BeatCounter 0x16f04334 BeatCounterAudioProcessor::~BeatCounterAudioProcessor() + 114 (BeatCounterAudioProcessor.cpp:45)  
> … way more stuff, snip

However, the thread that’s crashing is the one created by the MidiOutput object. That thread’s backtrace is very short:

> [@](#):
>
> Thread 9 Crashed:: midi out  
> 0 com.teragonaudio.BeatCounter 0x17641aff typeinfo name for juce::UnitTestRunner + 31

My code is really quite simple. I’ve tried #if 0’ing out pretty much everything except for creating the MIDI output in prepareToPlay(), and the following code in the destructor (btw it also crashes when moved to releaseResources()):

```auto
BeatCounterAudioProcessor::~BeatCounterAudioProcessor()
{
    if(virtualMidiOutput) {
        virtualMidiOutput->clearAllPendingMessages();
        virtualMidiOutput->stopBackgroundThread();
        delete virtualMidiOutput;
        virtualMidiOutput = nullptr;
    }
}
```

What’s going on here? Is this a totally bad idea to try starting a virtual MIDI output from a VST plugin? Or am I just missing something rather obvious?

---

<div class="post-metadata">

**Author:** ![nikreiman](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nikreiman/32/273_2.png) [@nikreiman](https://forum.juce.com/u/nikreiman)\
**Post date:** [February 14, 2013, 10:03pm UTC](https://forum.juce.com/t/plugin-with-midioutput-crashing-on-exit/9976/2 "2013-02-14T22:03:34Z")

</div>

BTW I should mention that this plugin is open source, if you want to see the full implementation it is [located on GitHub in a feature branch](https://github.com/teragonaudio/BeatCounter/blob/midi/Source/BeatCounterAudioProcessor.cpp).

---

<div class="post-metadata">

**Author:** ![nikreiman](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nikreiman/32/273_2.png) [@nikreiman](https://forum.juce.com/u/nikreiman)\
**Post date:** [March 11, 2013, 12:52pm UTC](https://forum.juce.com/t/plugin-with-midioutput-crashing-on-exit/9976/3 "2013-03-11T12:52:30Z")

</div>

_bump_, been nearly a month on this. Should I report a bug in Juce?

---

<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:** [March 11, 2013, 2:13pm UTC](https://forum.juce.com/t/plugin-with-midioutput-crashing-on-exit/9976/4 "2013-03-11T14:13:18Z")

</div>

> [@nikreiman](#):
>
> Should I report a bug in Juce?

If you can point to some juce code and show that it fails to do what it’s supposed to do, then I’ll fix it of course, but you’d have to prove that this isn’t your own bug.

But messing with midi devices in plugins when the host may also be using them is likely to be risky, so you’re really on your own with that!
