# Small Workaround for Crashing AU Plugins

**URL:** <https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006>\
**Category:** Audio Plugins\
**Created:** [August 30, 2019, 9:06am UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006 "2019-08-30T09:06:56Z")\
**Posts on this page:** 7\
**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 30, 2019, 9:06am UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006/1 "2019-08-30T09:06:57Z")

</div>

I’ve got some plugins that are crashing below because the `presets` array is `nullptr` even after they report a size of `8`. Adding a nullptr check seems to fix the problem:  
in `juce_AudioUnitPluginFormat.mm` from line `1207`

```auto
    int getNumPrograms() override
    {
        CFArrayRef presets;
        UInt32 sz = sizeof (CFArrayRef);
        int num = 0;

        if (AudioUnitGetProperty (audioUnit, kAudioUnitProperty_FactoryPresets,
                                  kAudioUnitScope_Global, 0, &presets, &sz) == noErr)
        {
            if (presets != nullptr)
            {
                num = (int) CFArrayGetCount (presets);
                CFRelease (presets);
            }
        }

        return num;
    }

```

---

<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 30, 2019, 9:28am UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006/2 "2019-08-30T09:28:12Z")

</div>

That sounds sensible, I’ll get it added. Thanks!

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [March 4, 2021, 5:14pm UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006/3 "2021-03-04T17:14:32Z")

</div>

I’d like to add a fix to the opposite call, setCurrentProgram:

```
void setCurrentProgram (int newIndex) override
{
    AUPreset current;
    current.presetNumber = newIndex;

    CFArrayRef presets;
    UInt32 sz = sizeof (CFArrayRef);

    if (AudioUnitGetProperty (audioUnit, kAudioUnitProperty_FactoryPresets,
                              kAudioUnitScope_Global, 0, &presets, &sz) == noErr)
    {
        if (CFArrayGetCount(presets) == 0) // <-- add this check!
            return;                   
        if (auto* p = (const AUPreset*) CFArrayGetValueAtIndex (presets, newIndex))
            current.presetName = p->presetName;

        CFRelease (presets);
    }

    AudioUnitSetProperty (audioUnit, kAudioUnitProperty_PresentPreset,
                          kAudioUnitScope_Global, 0, &current, sizeof (AUPreset));

    sendAllParametersChangedEvents();
}

```

If that check for an empty preset list isn’t added, the following line will just exit silently.

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [March 10, 2021, 10:23am UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006/4 "2021-03-10T10:23:49Z")

</div>

Any thoughts?

---

<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:** [March 10, 2021, 12:09pm UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006/5 "2021-03-10T12:09:41Z")

</div>

Where are you seeing this crash? Might it be better to do the following, so we don’t just early-return and still call `CFRelease()`, `sendAllParametersChangedEvents()` etc.:

```auto
void setCurrentProgram (int newIndex) override
{
    AUPreset current;
    current.presetNumber = newIndex;

    CFArrayRef presets;
    UInt32 sz = sizeof (CFArrayRef);

    if (AudioUnitGetProperty (audioUnit, kAudioUnitProperty_FactoryPresets,
                              kAudioUnitScope_Global, 0, &presets, &sz) == noErr)
    {
        if (CFArrayGetCount (presets) > 0)
            if (auto* p = (const AUPreset*) CFArrayGetValueAtIndex (presets, newIndex))
                current.presetName = p->presetName;

        CFRelease (presets);
    }

    AudioUnitSetProperty (audioUnit, kAudioUnitProperty_PresentPreset,
                          kAudioUnitScope_Global, 0, &current, sizeof (AUPreset));

    sendAllParametersChangedEvents();
}

```

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [March 11, 2021, 10:08am UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006/6 "2021-03-11T10:08:43Z")

</div>

That’s fine with me, thanks. Steps to reproduce this crash: use the JUCE plugin host, load an Audio Unit that has no programs (e.g. Gulfoss by Soundtheory). Calling getCurrentProgram() returns 0, but setCurrentProgram(0) leads to that crash. One could argue that it’s stupid to set a program when there are no programs, but still, it shouldn’t crash.

---

<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:** [March 11, 2021, 3:45pm UTC](https://forum.juce.com/t/small-workaround-for-crashing-au-plugins/35006/7 "2021-03-11T15:45:42Z")

</div>

Thanks for confirmiing, this is on `develop` now:

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