Fixes leak of AudioDeviceID array due to early return in AudioDeviceMac::GetNumberDevices()

Bug: webrtc:9348
Change-Id: I67a534ec8225180aa67018f7c11f1983262af585
Reviewed-on: https://webrtc-review.googlesource.com/80480
Commit-Queue: Henrik Andreassson <henrika@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23490}
This commit is contained in:
henrika 2018-06-01 13:13:03 +02:00 committed by Commit Bot
parent 392f8d0fa9
commit e97b5493a5

View File

@ -14,6 +14,7 @@
#include "rtc_base/arraysize.h"
#include "rtc_base/checks.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/ptr_util.h"
#include "system_wrappers/include/event_wrapper.h"
#include <ApplicationServices/ApplicationServices.h>
@ -1564,8 +1565,8 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope,
return 0;
}
AudioDeviceID* deviceIds = (AudioDeviceID*)malloc(size);
UInt32 numberDevices = size / sizeof(AudioDeviceID);
const auto deviceIds = rtc::MakeUnique<AudioDeviceID[]>(numberDevices);
AudioBufferList* bufferList = NULL;
UInt32 numberScopedDevices = 0;
@ -1596,8 +1597,9 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope,
// Then list the rest of the devices
bool listOK = true;
WEBRTC_CA_LOG_ERR(AudioObjectGetPropertyData(
kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, deviceIds));
WEBRTC_CA_LOG_ERR(AudioObjectGetPropertyData(kAudioObjectSystemObject,
&propertyAddress, 0, NULL, &size,
deviceIds.get()));
if (err != noErr) {
listOK = false;
} else {
@ -1641,25 +1643,13 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope,
}
if (!listOK) {
if (deviceIds) {
free(deviceIds);
deviceIds = NULL;
}
if (bufferList) {
free(bufferList);
bufferList = NULL;
}
return -1;
}
// Happy ending
if (deviceIds) {
free(deviceIds);
deviceIds = NULL;
}
return numberScopedDevices;
}