Skip to content

Commit 2a70e58

Browse files
authored
fix(capture): drain macOS autorelease pools between recording polls (#2980)
1 parent 035cbf9 commit 2a70e58

6 files changed

Lines changed: 480 additions & 393 deletions

File tree

‎crates/capture/src/ax.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,13 @@ impl Prober {
374374
.spawn(move || {
375375
for (job_session, target, x, y) in queue {
376376
if job_session == worker_session.load(Ordering::Acquire)
377-
&& let Some(hit) = lookup(x, y)
377+
&& let Some(hit) = {
378+
#[cfg(target_os = "macos")]
379+
let hit = objc2::rc::autoreleasepool(|_| lookup(x, y));
380+
#[cfg(not(target_os = "macos"))]
381+
let hit = lookup(x, y);
382+
hit
383+
}
378384
{
379385
worker_hits
380386
.lock()
@@ -497,6 +503,43 @@ mod tests {
497503
use super::*;
498504
use crate::model::*;
499505

506+
#[cfg(target_os = "macos")]
507+
#[test]
508+
fn the_prober_drains_autoreleased_objects_between_lookups() {
509+
use objc2::rc::{Retained, Weak};
510+
use objc2_foundation::NSObject;
511+
512+
fn lookup(_: f64, _: f64) -> Option<Hit> {
513+
thread_local! {
514+
static PREVIOUS: std::cell::RefCell<Option<Weak<NSObject>>> = const {
515+
std::cell::RefCell::new(None)
516+
};
517+
}
518+
let drained = PREVIOUS.with(|previous| {
519+
let drained = previous
520+
.borrow()
521+
.as_ref()
522+
.is_none_or(|weak| weak.load().is_none());
523+
let object = NSObject::new();
524+
*previous.borrow_mut() = Some(Weak::from_retained(&object));
525+
// Model a framework's temporary object, without screen or AX permissions.
526+
let _ = Retained::autorelease_ptr(object);
527+
drained
528+
});
529+
Some(Hit {
530+
role: if drained { "drained" } else { "leaked" }.into(),
531+
..Hit::default()
532+
})
533+
}
534+
535+
let prober = Prober::start(lookup);
536+
prober.probe(Target::Pointer(1), 0.0, 0.0);
537+
prober.probe(Target::Pointer(2), 0.0, 0.0);
538+
let hits = prober.finish();
539+
assert_eq!(hits.len(), 2);
540+
assert!(hits.values().all(|hit| hit.role == "drained"));
541+
}
542+
500543
#[test]
501544
fn labels_are_one_short_line_and_name_the_element() {
502545
assert_eq!(clip(" New\n Tab "), Some("New Tab".into()));

‎crates/capture/src/capture.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,12 +2285,14 @@ pub fn is_scrawl_app_running() -> bool {
22852285
{
22862286
use objc2_app_kit::NSRunningApplication;
22872287
use objc2_foundation::NSString;
2288-
let current_pid = std::process::id() as i32;
2289-
let apps = NSRunningApplication::runningApplicationsWithBundleIdentifier(
2290-
&NSString::from_str(SCRAWL_BUNDLE_ID),
2291-
);
2292-
apps.iter()
2293-
.any(|app| app.processIdentifier() != current_pid && !app.isTerminated())
2288+
objc2::rc::autoreleasepool(|_| {
2289+
let current_pid = std::process::id() as i32;
2290+
let apps = NSRunningApplication::runningApplicationsWithBundleIdentifier(
2291+
&NSString::from_str(SCRAWL_BUNDLE_ID),
2292+
);
2293+
apps.iter()
2294+
.any(|app| app.processIdentifier() != current_pid && !app.isTerminated())
2295+
})
22942296
}
22952297
#[cfg(not(target_os = "macos"))]
22962298
{

0 commit comments

Comments
 (0)