Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: drain autoreleased objects in macOS camera enumeration

Richie McIlroy012d202652c9
diff --git a/crates/camera-avfoundation/src/lib.rs b/crates/camera-avfoundation/src/lib.rs
index 00121793f..dcbc84a05 100644
--- a/crates/camera-avfoundation/src/lib.rs
+++ b/crates/camera-avfoundation/src/lib.rs
@@ -11,36 +11,41 @@ use std::{
};
use tracing::warn;
+// Pool-wrapped: device polling calls this every few seconds from tokio threads
+// that have no ambient NSAutoreleasePool, so the discovery session's
+// autoreleased temporaries would otherwise leak for the process lifetime.
pub fn list_video_devices() -> arc::R<ns::Array<av::CaptureDevice>> {
- let mut device_types = vec![av::CaptureDeviceType::built_in_wide_angle_camera()];
+ objc::ar_pool(|| {
+ let mut device_types = vec![av::CaptureDeviceType::built_in_wide_angle_camera()];
- if api::macos_available("13.0")
- && let Some(typ) = unsafe { av::CaptureDeviceType::desk_view_camera() }
- {
- device_types.push(typ);
- }
-
- if api::macos_available("14.0") {
- if let Some(typ) = unsafe { av::CaptureDeviceType::external() } {
+ if api::macos_available("13.0")
+ && let Some(typ) = unsafe { av::CaptureDeviceType::desk_view_camera() }
+ {
device_types.push(typ);
}
- if let Some(typ) = unsafe { av::CaptureDeviceType::continuity_camera() } {
- device_types.push(typ);
+
+ if api::macos_available("14.0") {
+ if let Some(typ) = unsafe { av::CaptureDeviceType::external() } {
+ device_types.push(typ);
+ }
+ if let Some(typ) = unsafe { av::CaptureDeviceType::continuity_camera() } {
+ device_types.push(typ);
+ }
+ } else {
+ device_types.push(av::CaptureDeviceType::external_unknown());
}
- } else {
- device_types.push(av::CaptureDeviceType::external_unknown());
- }
- let device_types = ns::Array::from_slice(&device_types);
+ let device_types = ns::Array::from_slice(&device_types);
- let video_discovery_session =
- av::CaptureDeviceDiscoverySession::with_device_types_media_and_pos(
- &device_types,
- Some(av::MediaType::video()),
- av::CaptureDevicePos::Unspecified,
- );
+ let video_discovery_session =
+ av::CaptureDeviceDiscoverySession::with_device_types_media_and_pos(
+ &device_types,
+ Some(av::MediaType::video()),
+ av::CaptureDevicePos::Unspecified,
+ );
- video_discovery_session.devices()
+ video_discovery_session.devices()
+ })
}
#[derive(Clone, Copy)]
diff --git a/crates/camera/src/macos.rs b/crates/camera/src/macos.rs
index be724e437..9fb729474 100644
--- a/crates/camera/src/macos.rs
+++ b/crates/camera/src/macos.rs
@@ -5,16 +5,21 @@ use cidre::*;
use objc2_av_foundation::*;
pub(super) fn list_cameras_impl() -> impl Iterator<Item = CameraInfo> {
- let devices = cap_camera_avfoundation::list_video_devices();
- devices
- .iter()
- .map(|d| CameraInfo {
- device_id: d.unique_id().to_string(),
- model_id: ModelID::from_avfoundation(d),
- display_name: d.localized_name().to_string(),
- })
- .collect::<Vec<_>>()
- .into_iter()
+ // ar_pool: called from pool-less tokio threads on a polling cadence; the
+ // unique_id/localized_name accessors autorelease NSStrings that would
+ // otherwise accumulate for the process lifetime.
+ objc::ar_pool(|| {
+ let devices = cap_camera_avfoundation::list_video_devices();
+ devices
+ .iter()
+ .map(|d| CameraInfo {
+ device_id: d.unique_id().to_string(),
+ model_id: ModelID::from_avfoundation(d),
+ display_name: d.localized_name().to_string(),
+ })
+ .collect::<Vec<_>>()
+ })
+ .into_iter()
}
impl CameraInfo {