diff --git a/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm b/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm index 2eaa9da0a7..0c7b1c7df1 100644 --- a/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm +++ b/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm @@ -216,13 +216,40 @@ namespace AudioUnitFormatHelpers if (manuString != nullptr && CFGetTypeID (manuString) == CFStringGetTypeID()) manufacturer = String::fromCFString ((CFStringRef) manuString); - const ResFileRefNum resFileId = CFBundleOpenBundleResourceMap (bundleRef.get()); - UseResFile (resFileId); + class ScopedBundleResourceMap final + { + public: + explicit ScopedBundleResourceMap (CFBundleRef refIn) : ref (refIn), + resFileId (CFBundleOpenBundleResourceMap (ref)), + valid (resFileId != -1) + { + if (valid) + UseResFile (resFileId); + } + + ~ScopedBundleResourceMap() + { + if (valid) + CFBundleCloseBundleResourceMap (ref, resFileId); + } + + bool isValid() const noexcept + { + return valid; + } + + private: + const CFBundleRef ref; + const ResFileRefNum resFileId; + const bool valid; + }; + + const ScopedBundleResourceMap resourceMap { bundleRef.get() }; const OSType thngType = stringToOSType ("thng"); auto numResources = Count1Resources (thngType); - if (numResources > 0) + if (resourceMap.isValid() && numResources > 0) { for (ResourceIndex i = 1; i <= numResources; ++i) { @@ -268,8 +295,6 @@ namespace AudioUnitFormatHelpers [bundle release]; } - - CFBundleCloseBundleResourceMap (bundleRef.get(), resFileId); } }