@@ -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( ) ) ) ;
0 commit comments