Skip to content

Commit 38907cb

Browse files
authored
Fix get_device_name memory leak by using CFRetained for device name (#147)
I would've used CFRelease, but that isn't a public function. CFRetained calls CFRelease under the hood when it's dropped, so this is the next best thing. I noticed this leak when running instruments on Zed, please let me know if I missed anything.
1 parent d0545fc commit 38907cb

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/audio_unit/macos_helpers.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use objc2_core_audio::{
3030
AudioObjectSetPropertyData, AudioStreamRangedDescription,
3131
};
3232
use objc2_core_audio_types::{AudioBufferList, AudioStreamBasicDescription, AudioValueRange};
33-
use objc2_core_foundation::CFString;
33+
use objc2_core_foundation::{CFRetained, CFString};
3434

3535
use crate::audio_unit::audio_format::{AudioFormat, LinearPcmFlags};
3636
use crate::audio_unit::sample_format::SampleFormat;
@@ -290,8 +290,9 @@ pub fn get_device_name(device_id: AudioDeviceID) -> Result<String, Error> {
290290
NonNull::from(&mut device_name).cast(),
291291
);
292292
try_status_or_return!(status);
293-
294-
Ok((&*device_name).to_string())
293+
let device_name = NonNull::new(device_name as *mut CFString).ok_or(Error::Unknown(0))?;
294+
let device_name = CFRetained::from_raw(device_name);
295+
Ok(device_name.to_string())
295296
}
296297
}
297298

0 commit comments

Comments
 (0)