1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-19 14:17:41 +02:00

Do CFRelease only inside destructors, in AudioUnitEffects.cpp ...

... may fix a memory leak in AudioUnitEffect::SupportsAutomation
This commit is contained in:
Paul Licameli 2016-08-07 12:04:49 -04:00
parent eb6ba1ed7a
commit 0bb17c174e

View File

@ -42,6 +42,22 @@
#include "AudioUnitEffect.h" #include "AudioUnitEffect.h"
struct CFReleaser
{ void operator () (const void *p) const { if (p) CFRelease(p); } };
template <typename T>
using CFunique_ptr = std::unique_ptr<T, CFReleaser>;
struct AudioUnitParameterInfoEx : AudioUnitParameterInfo
{
~AudioUnitParameterInfoEx ()
{
if ((flags & kAudioUnitParameterFlag_HasCFNameString)
&&
(flags & kAudioUnitParameterFlag_CFNameRelease))
CFRelease( cfNameString );
}
};
// ============================================================================ // ============================================================================
// Module registration entry point // Module registration entry point
// //
@ -228,6 +244,8 @@ void AudioUnitEffectsModule::LoadAudioUnitsOfType(OSType inAUType,
{ {
CFStringRef cfName; CFStringRef cfName;
result = AudioComponentCopyName(component, &cfName); result = AudioComponentCopyName(component, &cfName);
CFunique_ptr<const __CFString> uName{ cfName };
if (result == noErr) if (result == noErr)
{ {
wxString name = wxCFStringRef::AsString(cfName); wxString name = wxCFStringRef::AsString(cfName);
@ -237,8 +255,6 @@ void AudioUnitEffectsModule::LoadAudioUnitsOfType(OSType inAUType,
FromOSType(found.componentType).c_str(), FromOSType(found.componentType).c_str(),
FromOSType(found.componentSubType).c_str(), FromOSType(found.componentSubType).c_str(),
name.c_str())); name.c_str()));
CFRelease(cfName);
} }
} }
@ -566,27 +582,26 @@ void AudioUnitEffectExportDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
&size); &size);
// And convert it to XML // And convert it to XML
CFDataRef xml = CFPropertyListCreateXMLData(kCFAllocatorDefault, CFunique_ptr<const __CFData> xml {
content); CFPropertyListCreateXMLData(kCFAllocatorDefault, content)
};
if (xml) if (xml)
{ {
// Create the CFURL for the path // Create the CFURL for the path
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFunique_ptr<const __CFURL> url {
CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
wxCFStringRef(path), wxCFStringRef(path),
kCFURLPOSIXPathStyle, kCFURLPOSIXPathStyle,
false); false)
};
if (url) if (url)
{ {
SInt32 error; SInt32 error;
Boolean res = CFURLWriteDataAndPropertiesToResource(url, Boolean res = CFURLWriteDataAndPropertiesToResource(url.get(),
xml, xml.get(),
NULL, NULL,
&error); &error);
CFRelease(url);
} }
// Get rid of the XML data
CFRelease(xml);
} }
// And continue to the next selected preset // And continue to the next selected preset
@ -730,10 +745,13 @@ void AudioUnitEffectImportDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
mList->GetItemText(sel).c_str()); mList->GetItemText(sel).c_str());
// Create the CFURL for the path // Create the CFURL for the path
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFunique_ptr<const __CFURL> url {
CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
wxCFStringRef(path), wxCFStringRef(path),
kCFURLPOSIXPathStyle, kCFURLPOSIXPathStyle,
false); false)
};
if (!url) if (!url)
{ {
continue; continue;
@ -742,24 +760,24 @@ void AudioUnitEffectImportDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
CFDataRef xml; CFDataRef xml;
SInt32 error; SInt32 error;
Boolean res = CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, Boolean res = CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,
url, url.get(),
&xml, &xml,
NULL, NULL,
NULL, NULL,
&error); &error);
CFRelease(url); CFunique_ptr<const __CFData> uxml { xml };
if (!res) if (!res)
{ {
continue; continue;
} }
CFPropertyListRef content; CFunique_ptr<char> content {
content = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (char*)CFPropertyListCreateFromXMLData(kCFAllocatorDefault,
xml, xml,
kCFPropertyListImmutable, kCFPropertyListImmutable,
NULL); NULL)
CFRelease(xml); };
if (!content) if (!content)
{ {
@ -772,7 +790,6 @@ void AudioUnitEffectImportDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
0, 0,
&content, &content,
sizeof(content)); sizeof(content));
CFRelease(content);
mEffect->SaveUserPreset(mEffect->mHost->GetUserPresetsGroup(mList->GetItemText(sel))); mEffect->SaveUserPreset(mEffect->mHost->GetUserPresetsGroup(mList->GetItemText(sel)));
} }
@ -961,7 +978,7 @@ bool AudioUnitEffect::SupportsAutomation()
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
{ {
AudioUnitParameterInfo info; AudioUnitParameterInfoEx info;
dataSize = sizeof(info); dataSize = sizeof(info);
result = AudioUnitGetProperty(mUnit, result = AudioUnitGetProperty(mUnit,
kAudioUnitProperty_ParameterInfo, kAudioUnitProperty_ParameterInfo,
@ -1501,7 +1518,7 @@ bool AudioUnitEffect::GetAutomationParameters(EffectAutomationParameters & parms
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
{ {
AudioUnitParameterInfo info; AudioUnitParameterInfoEx info;
dataSize = sizeof(info); dataSize = sizeof(info);
result = AudioUnitGetProperty(mUnit, result = AudioUnitGetProperty(mUnit,
kAudioUnitProperty_ParameterInfo, kAudioUnitProperty_ParameterInfo,
@ -1519,10 +1536,6 @@ bool AudioUnitEffect::GetAutomationParameters(EffectAutomationParameters & parms
if (info.flags & kAudioUnitParameterFlag_HasCFNameString) if (info.flags & kAudioUnitParameterFlag_HasCFNameString)
{ {
name = wxCFStringRef::AsString(info.cfNameString); name = wxCFStringRef::AsString(info.cfNameString);
if (info.flags & kAudioUnitParameterFlag_CFNameRelease)
{
CFRelease(info.cfNameString);
}
} }
if (name.IsEmpty()) if (name.IsEmpty())
@ -1583,7 +1596,7 @@ bool AudioUnitEffect::SetAutomationParameters(EffectAutomationParameters & parms
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
{ {
AudioUnitParameterInfo info; AudioUnitParameterInfoEx info;
dataSize = sizeof(info); dataSize = sizeof(info);
result = AudioUnitGetProperty(mUnit, result = AudioUnitGetProperty(mUnit,
kAudioUnitProperty_ParameterInfo, kAudioUnitProperty_ParameterInfo,
@ -1601,10 +1614,6 @@ bool AudioUnitEffect::SetAutomationParameters(EffectAutomationParameters & parms
if (info.flags & kAudioUnitParameterFlag_HasCFNameString) if (info.flags & kAudioUnitParameterFlag_HasCFNameString)
{ {
name = wxCFStringRef::AsString(info.cfNameString); name = wxCFStringRef::AsString(info.cfNameString);
if (info.flags & kAudioUnitParameterFlag_CFNameRelease)
{
CFRelease(info.cfNameString);
}
} }
if (name.IsEmpty()) if (name.IsEmpty())
@ -1669,6 +1678,7 @@ bool AudioUnitEffect::LoadFactoryPreset(int id)
0, 0,
&array, &array,
&dataSize); &dataSize);
CFunique_ptr < const __CFArray > uarray { array };
if (result != noErr) if (result != noErr)
{ {
return false; return false;
@ -1697,8 +1707,6 @@ bool AudioUnitEffect::LoadFactoryPreset(int id)
AUParameterListenerNotify(NULL, NULL, &aup); AUParameterListenerNotify(NULL, NULL, &aup);
} }
CFRelease(array);
return result == noErr; return result == noErr;
} }
@ -1721,6 +1729,7 @@ wxArrayString AudioUnitEffect::GetFactoryPresets()
0, 0,
&array, &array,
&dataSize); &dataSize);
CFunique_ptr< const __CFArray > uarray { array };
if (result == noErr) if (result == noErr)
{ {
for (CFIndex i = 0, cnt = CFArrayGetCount(array); i < cnt; i++) for (CFIndex i = 0, cnt = CFArrayGetCount(array); i < cnt; i++)
@ -1728,7 +1737,6 @@ wxArrayString AudioUnitEffect::GetFactoryPresets()
AUPreset *preset = (AUPreset *) CFArrayGetValueAtIndex(array, i); AUPreset *preset = (AUPreset *) CFArrayGetValueAtIndex(array, i);
presets.Add(wxCFStringRef::AsString(preset->presetName)); presets.Add(wxCFStringRef::AsString(preset->presetName));
} }
CFRelease(array);
} }
return presets; return presets;