From 3fdd4526467caa6e4253abc67545defc6a00743d Mon Sep 17 00:00:00 2001 From: Benjamin Klum Date: Sun, 16 Oct 2022 22:35:03 +0200 Subject: [PATCH 1/2] #33 Fix AVFoundation crash on macOS when getting voices by preventing manual cleanup of non-owned objects --- src/backends/av_foundation.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/backends/av_foundation.rs b/src/backends/av_foundation.rs index a64d9b4..737e90b 100644 --- a/src/backends/av_foundation.rs +++ b/src/backends/av_foundation.rs @@ -4,6 +4,7 @@ use std::{str::FromStr, sync::Mutex}; use cocoa_foundation::base::{id, nil, NO}; use cocoa_foundation::foundation::NSString; use core_foundation::array::CFArray; +use core_foundation::base::TCFType; use core_foundation::string::CFString; use lazy_static::lazy_static; use log::{info, trace}; @@ -290,19 +291,26 @@ impl Backend for AvFoundation { } fn voices(&self) -> Result, Error> { - let voices: CFArray = unsafe { msg_send![class!(AVSpeechSynthesisVoice), speechVoices] }; + let voices: CFArray = unsafe { + CFArray::wrap_under_get_rule(msg_send![class!(AVSpeechSynthesisVoice), speechVoices]) + }; let rv = voices .iter() - .map(|v| { - let id: CFString = unsafe { msg_send![*v as *const Object, identifier] }; - let name: CFString = unsafe { msg_send![*v as *const Object, name] }; + .map(|v| unsafe { + let id: CFString = unsafe { + CFString::wrap_under_get_rule(msg_send![*v as *const Object, identifier]) + }; + let name: CFString = + unsafe { CFString::wrap_under_get_rule(msg_send![*v as *const Object, name]) }; let gender: i64 = unsafe { msg_send![*v as *const Object, gender] }; let gender = match gender { 1 => Some(Gender::Male), 2 => Some(Gender::Female), _ => None, }; - let language: CFString = unsafe { msg_send![*v as *const Object, language] }; + let language: CFString = unsafe { + CFString::wrap_under_get_rule(msg_send![*v as *const Object, language]) + }; let language = language.to_string(); let language = LanguageIdentifier::from_str(&language).unwrap(); Voice { From ddf96c10aa423c0541a049853a76ea78625dd4cb Mon Sep 17 00:00:00 2001 From: Benjamin Klum Date: Sun, 16 Oct 2022 23:15:52 +0200 Subject: [PATCH 2/2] #33 Remove unnecessary unsafe keyword --- src/backends/av_foundation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backends/av_foundation.rs b/src/backends/av_foundation.rs index 737e90b..06ebe78 100644 --- a/src/backends/av_foundation.rs +++ b/src/backends/av_foundation.rs @@ -296,7 +296,7 @@ impl Backend for AvFoundation { }; let rv = voices .iter() - .map(|v| unsafe { + .map(|v| { let id: CFString = unsafe { CFString::wrap_under_get_rule(msg_send![*v as *const Object, identifier]) };