Skip to main content

Tracking Down a Core Foundation Memory Leak in JUCE's Audio Unit Wrapper

A leak check flagged CFStrings in a JUCE Audio Unit. A minimal AudioToolbox host traced them to three ownership errors in the AU wrapper, and showed why the obvious fix would have created dangling pointers. Reported upstream as JUCE issue #1792.

Stephane Pericat

Founder & Lead Developer · November 13, 2026 · 9 min read

On this page: Where it showed up

During memory qualification of an Audio Unit plugin, a stack-logged leak check kept reporting the same thing: a few CFStrings, allocated inside JUCE's AU wrapper, that nothing ever released. The amounts were tiny, 64 bytes per string. But a leak that appears in every report is worth understanding, and this one turned out to be a compact lesson in Core Foundation ownership, including a fix that looks correct and is not.

We reported it to JUCE as issue #1792, with a reproduction and a tested patch. When we last checked, on November 14, 2026, the issue was open and the patch had not been merged. Everything below about the fix is our own validation, not an accepted upstream change.

Where it showed up

Our memory gate for the plugin runs a test host that opens and closes the editor 150 times, then runs the process under leaks with stack logging. One criterion, fixed before any run, was that no leaked allocation may have a frame in the plugin binary. JUCE's wrapper code is compiled into the plugin, so it counts.

Every AU report contained the same entry, five leaked CFStrings allocated during AU construction in juce::String::toCFString(). The VST3 build had none. That pointed at the AU wrapper rather than at our code.

There is a second lesson in how we nearly missed it. The first version of our report parser read this criterion as a pass. It looked for the plugin's name where leaks prints bundle identifiers, and it searched the Leak: entries while the stacks are printed in separate STACK OF … ROOT LEAK sections. Only after we fixed the parser, and checked that the gate could fail, did the leak appear.

Core Foundation ownership, briefly

Core Foundation objects are reference counted, and the convention is in the function name:

  • Functions with Create or Copy in their name return an object you own, a +1 reference that you must balance with CFRelease.
  • Functions with Get return an object you do not own.
  • Audio Unit properties that hand a CF object to the host follow the Create rule: the host owns what it receives. Apple's AudioUnitProperties.h states it "as with ALL properties that retrieve a CF object from an audio unit".

Two details matter here:

  • JUCE's String::toCFString() is a Create function by behaviour, not by name. It calls CFStringCreateWithBytes and returns that +1 reference.
  • JUCE stores CFStrings in an OwnedArray<const __CFString>, whose delete policy calls CFRelease once per element. The array adopts exactly one reference per string.

There is also a trap for anyone writing a reproduction. On arm64, short ASCII strings are often tagged pointers: the characters are encoded in the pointer itself, so nothing is allocated and leaks never reports them. In our test, "Off" and "On" were tagged and longer names were heap objects. A reproduction needs value names longer than about eleven characters.

A minimal reproduction

We wanted the smallest setup that could show the leak without a DAW, our plugin or any editor.

The plugin is a JUCE AU with one AudioParameterChoice (three long value names such as "A long first choice name", plus "Off"), one AudioParameterBool, three factory programs named "A long factory program name 1" to 3, and no editor. An environment variable, read at run time, makes setCurrentProgram() call updateHostDisplay (ChangeDetails().withProgramChanged (true)), as plugins with a preset menu often do.

The host is a plain C program using AudioToolbox. For each instance it:

  1. creates and initializes the AU;
  2. reads the parameter info, value strings and string-from-value, releasing whatever the API says the caller owns;
  3. reads the factory presets, selects one through kAudioUnitProperty_PresentPreset N times, then reads the present preset and releases its name;
  4. round-trips the state through kAudioUnitProperty_ClassInfo;
  5. uninitializes and disposes the instance.

Releasing what the caller owns matters. A host that leaks its own references pollutes the report with leaks that are not the plugin's.

The run is MallocStackLogging=1 leaks --atExit -- ./host. Each build variant gets its own AU subtype, so the system's component cache cannot load the wrong build.

With four instances, on JUCE 9.0.3, macOS 26.6.2, arm64:

ConfigurationLeaked CFStringsBytes
No preset selections12768
5 host preset selections per instance322,048
The same, with the plugin reporting each program change523,328

The stacks split the 52 into three groups:

12 ×  AudioComponentInstanceNew → … → JuceAU::JuceAU
        → JuceAU::addParameters()
        → juce::String::toCFString()

20 ×  AudioUnitSetProperty → … → JuceAU::NewFactoryPresetSet
        → juce::String::toCFString()

20 ×  … → JuceAU::NewFactoryPresetSet → <processor>::setCurrentProgram
        → AudioProcessor::updateHostDisplay
        → JuceAU::AudioProcessorChangedUpdater::handleAsyncUpdate
        → juce::String::toCFString()   (refreshCurrentPreset, inlined)

That is 3 long value names per instance (4 × 3 = 12), one preset name per host selection (4 × 5 = 20), and one more per selection when the plugin notifies the host (another 20).

Root cause

All three sites are in juce_audio_plugin_client_AU_1.mm. The links below point to develop at commit b5d9b21c; the file was identical in JUCE 9.0.3 and 9.0.5.

Parameter value strings: a copy that is really a retain

addParameters() builds the value-name list for each discrete parameter:

stringValues->add (CFStringCreateCopy (
                       nullptr, (getTextValue (value).toCFString())));

(JUCE's code, with our line breaks.)

toCFString() returns a +1 string. CFStringCreateCopy looks like a defensive copy, but for an immutable string Core Foundation returns the same object with its reference count incremented. The OwnedArray later releases one reference, and the reference from toCFString() is never released. A JUCE-free check shows the counts:

created:             retainCount 1
CFStringCreateCopy:  same object: yes, retainCount 2
after one CFRelease: retainCount 1   → never freed

(CFGetRetainCount is fine for a demonstration like this, and unreliable as program logic.)

Preset names: a +1 handed to code that retains its own

NewFactoryPresetSet() runs when the host selects a factory preset. refreshCurrentPreset() runs when the plugin reports a program change. Both fill an AUPreset with presetName = …toCFString() and pass it to AUBase::SetAFactoryPresetAsCurrent. The Audio Unit SDK's implementation takes its own reference:

CFRelease(mCurrentPreset.presetName);
mCurrentPreset = inPreset;
CFRetain(mCurrentPreset.presetName);

AUBase balances its own retain when the preset changes again, but nothing balances the +1 from toCFString(). In a plugin whose setCurrentProgram() notifies the host, one host selection runs both paths and leaks two strings.

The lifetime trap

The obvious fix is three lines: drop CFStringCreateCopy, and add a CFRelease after each SetAFactoryPresetAsCurrent. It removes every leak. It also introduces a dangling pointer.

The value strings are handed to the host by GetParameterValueStrings():

*outStrings = CFArrayCreate (
    nullptr,
    (const void**) valueStrings->getRawDataPointer(),
    valueStrings->size(),
    nullptr);   // callbacks

(Line breaks and comment ours.)

The last argument is the array's callbacks. With nullptr, the array does not retain the strings it contains; it holds raw pointers. The host owns the array and may keep it, for example to build a menu, for as long as it likes, including after the plugin instance is gone.

In the stock code, the leak hides this. The strings carry an extra reference that is never released, so they outlive the instance, and a host's array still points at valid strings. Fix only the ownership, and the OwnedArray now releases the last reference when the instance is destroyed. Any array the host kept is left pointing at freed memory.

We checked this directly. The reproduction host has a mode that keeps one value-string array past AudioComponentInstanceDispose and then inspects each element with malloc_size, under MallocScribble. That is a heuristic, but a decisive one here:

BuildHeap value strings in the kept array, after dispose
Stock JUCEstill allocated (because they leak)
Ownership fix onlyfreed: the host's array points at freed memory
Proposed fixstill allocated: the array holds its own references

The proposed fix passes &kCFTypeArrayCallBacks, so the array retains each string when it is created and releases them when the host releases the array. That is the contract a host expects from any CF collection it is given.

The proposed patch

The patch in the issue has four hunks. Simplified here, with shortened names (the exact diff is in the issue):

@@ GetParameterValueStrings
     *outStrings = CFArrayCreate (nullptr, values, count,
-                                 nullptr);
+                                 &kCFTypeArrayCallBacks);
@@ NewFactoryPresetSet
     SetAFactoryPresetAsCurrent (chosenPreset);  // AUBase retains the name
+    CFRelease (chosenPreset.presetName);
@@ addParameters
-    stringValues->add (CFStringCreateCopy (nullptr, text.toCFString()));
+    stringValues->add (text.toCFString());      // the array adopts the +1
@@ refreshCurrentPreset
     SetAFactoryPresetAsCurrent (currentPreset); // AUBase retains the name
+    CFRelease (currentPreset.presetName);

In words: let the OwnedArray adopt the existing +1 instead of adding a second one, release the preset name once AUBase has taken its own reference, and make the returned array retain its contents.

Validation

We built the reproduction three times, against stock JUCE, against a patched copy with only the ownership changes, and against a patched copy with the proposed fix. The JUCE checkout itself was never modified.

  • Leaks: 0 with either fix, in all three configurations (no presets, host selections, plugin notifications).
  • Behaviour: parameter info, value strings, string-from-value and factory preset names were diffed across builds and identical. The present preset's name after a selection read back correctly; that is the string whose ownership the patch changes.
  • State: a round trip that can fail (set the choice to 3, save ClassInfo, change it to 0, restore, read 3 back) passed in every build.
  • Lifetime: only the proposed fix keeps a host-held value-string array valid after the instance is disposed.
  • Validation: auval reported AU VALIDATION SUCCEEDED for all three builds.
  • Versions: first reproduced against JUCE 9.0.3. Before filing, we rebuilt the reproduction against develop at b5d9b21c rather than inferring from a diff. The counts were the same (12 / 32 / 52 stock, 0 patched), and the patch applied cleanly.

These results come from a minimal reproduction, an AudioToolbox host and auval. We have not tested the patch inside commercial DAWs, and it is not an accepted JUCE fix. We have not applied a local JUCE patch to our product builds either. The leak is bounded per event, a few hundred bytes per plugin instance and 64 bytes per preset change, so we are waiting for the upstream resolution.

Upstream status

Status as last checked, on November 14, 2026:

  • JUCE issue #1792 was open, with no maintainer response yet.
  • juce_audio_plugin_client_AU_1.mm on develop had not changed since September 2026, so the leak sites were still present. The latest release, 9.0.5, did not include a fix.
  • A separate issue from another reporter, #1796, about the AU present preset after instantiation, references #1792. It notes that its own proposed change would leak one more preset name per instance until #1792 is fixed, and that its runs leaked nothing with #1792's two CFRelease lines applied on top. That corroborates part of the patch; it is not an upstream acceptance.

If you maintain a JUCE AU with long value names on discrete parameters, or long factory preset names, you are affected to the same small degree. Check the issue for the current status before patching anything locally.

Lessons

  • Write down ownership at every framework boundary. Here a C++ container, a JUCE helper and Apple's SDK each held references, and each was correct in isolation. The bug was in how they were combined.
  • A redundant-looking copy can leak. CFStringCreateCopy on an immutable string is a retain with a different name.
  • Fixing a leak can expose a use-after-free. A leak can mask a missing retain. Before releasing anything, check what else holds a raw pointer to it, especially anything already handed to a caller.
  • Build the smallest host that exercises the API, and make it release exactly what the API says the caller owns. AudioToolbox is enough to create instances, select presets and round-trip state without a DAW.
  • Test lifetime, not just leaks. Zero leaks is not the goal; correct lifetime is. Keeping an object past its producer's lifetime catches over-release that a leak check never will.
  • Use long test strings. Tagged pointers hide short-string leaks entirely.
  • Make sure your leak gate can fail. Ours read as a pass until we fixed its parser.
  • Send upstream a tested patch with its evidence: reproduction, counts before and after, behavioural checks, and confirmation against the development branch, with private details removed.

A later note from the same qualification work covers WebKit helper processes in a WebView plugin editor. Our notes on JUCE plugin validation and real-time safety are part of the JUCE skill in our open agent skills, which we wrote about in Teaching Agents to Think in Signal Flow.

Back to all posts