Skip to content

Commit ecddbd7

Browse files
committed
Properly drop the top-level adapter struct
1 parent 40efae9 commit ecddbd7

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

platforms/unix/src/adapter.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ impl TreeChangeHandler for AdapterChangeHandler<'_> {
198198
}
199199
}
200200

201-
static NEXT_ADAPTER_ID: AtomicUsize = AtomicUsize::new(0);
202-
203201
pub(crate) struct AdapterImpl {
204202
id: usize,
205203
messages: Sender<Message>,
@@ -208,13 +206,13 @@ pub(crate) struct AdapterImpl {
208206

209207
impl AdapterImpl {
210208
fn new(
209+
id: usize,
211210
initial_state: TreeUpdate,
212211
is_window_focused: bool,
213212
root_window_bounds: WindowBounds,
214213
action_handler: Box<dyn ActionHandler + Send>,
215214
) -> Self {
216215
let tree = Tree::new(initial_state, is_window_focused);
217-
let id = NEXT_ADAPTER_ID.fetch_add(1, Ordering::SeqCst);
218216
let (messages, context) = {
219217
let mut app_context = AppContext::write();
220218
let messages = app_context.messages.clone().unwrap();
@@ -430,7 +428,10 @@ impl Drop for AdapterImpl {
430428

431429
pub(crate) type LazyAdapter = Pin<Arc<Lazy<AdapterImpl, Boxed<AdapterImpl>>>>;
432430

431+
static NEXT_ADAPTER_ID: AtomicUsize = AtomicUsize::new(0);
432+
433433
pub struct Adapter {
434+
id: usize,
434435
r#impl: LazyAdapter,
435436
is_window_focused: Arc<AtomicBool>,
436437
root_window_bounds: Arc<Mutex<WindowBounds>>,
@@ -442,6 +443,7 @@ impl Adapter {
442443
source: impl 'static + FnOnce() -> TreeUpdate + Send,
443444
action_handler: Box<dyn ActionHandler + Send>,
444445
) -> Self {
446+
let id = NEXT_ADAPTER_ID.fetch_add(1, Ordering::SeqCst);
445447
let is_window_focused = Arc::new(AtomicBool::new(false));
446448
let is_window_focused_copy = is_window_focused.clone();
447449
let root_window_bounds = Arc::new(Mutex::new(Default::default()));
@@ -451,6 +453,7 @@ impl Adapter {
451453
let is_window_focused = is_window_focused_copy.load(Ordering::Relaxed);
452454
let root_window_bounds = *root_window_bounds_copy.lock().unwrap();
453455
AdapterImpl::new(
456+
id,
454457
source(),
455458
is_window_focused,
456459
root_window_bounds,
@@ -460,11 +463,12 @@ impl Adapter {
460463
.boxed(),
461464
));
462465
let adapter = Self {
466+
id,
463467
r#impl: r#impl.clone(),
464468
is_window_focused,
465469
root_window_bounds,
466470
};
467-
block_on(async move { ActivationContext::activate_eventually(r#impl).await });
471+
block_on(async move { ActivationContext::activate_eventually(id, r#impl).await });
468472
adapter
469473
}
470474

@@ -496,6 +500,14 @@ impl Adapter {
496500
}
497501
}
498502

503+
impl Drop for Adapter {
504+
fn drop(&mut self) {
505+
block_on(async {
506+
ActivationContext::remove_adapter(self.id).await;
507+
})
508+
}
509+
}
510+
499511
pub(crate) enum Message {
500512
RegisterInterfaces {
501513
adapter_id: usize,

platforms/unix/src/context.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl AppContext {
116116

117117
pub(crate) struct ActivationContext {
118118
_task: Task<()>,
119-
adapters: Vec<LazyAdapter>,
119+
adapters: Vec<(usize, LazyAdapter)>,
120120
}
121121

122122
static ACTIVATION_CONTEXT: AsyncOnceCell<Arc<AsyncMutex<ActivationContext>>> = AsyncOnceCell::new();
@@ -143,15 +143,27 @@ impl ActivationContext {
143143
.await
144144
}
145145

146-
pub(crate) async fn activate_eventually(adapter: LazyAdapter) {
146+
pub(crate) async fn activate_eventually(id: usize, adapter: LazyAdapter) {
147147
let mut activation_context = ActivationContext::get_or_init().await;
148-
activation_context.adapters.push(adapter);
148+
activation_context.adapters.push((id, adapter));
149149
let is_a11y_enabled = AppContext::get_or_init().messages.is_some();
150150
if is_a11y_enabled {
151-
let adapter = activation_context.adapters.last().unwrap();
151+
let adapter = &activation_context.adapters.last().unwrap().1;
152152
adapter.as_ref().await;
153153
}
154154
}
155+
156+
pub(crate) async fn remove_adapter(id: usize) {
157+
if let Some(activation_context) = ACTIVATION_CONTEXT.get() {
158+
let mut context = activation_context.lock().await;
159+
if let Ok(index) = context
160+
.adapters
161+
.binary_search_by(|adapter| adapter.0.cmp(&id))
162+
{
163+
context.adapters.remove(index);
164+
}
165+
}
166+
}
155167
}
156168

157169
async fn listen(session_bus: Connection) -> zbus::Result<()> {
@@ -182,7 +194,7 @@ async fn listen(session_bus: Connection) -> zbus::Result<()> {
182194
if atspi_bus.is_some() {
183195
if let Some(activation_context) = ACTIVATION_CONTEXT.get() {
184196
let activation_context = activation_context.lock().await;
185-
for adapter in &activation_context.adapters {
197+
for (_, adapter) in &activation_context.adapters {
186198
adapter.as_ref().await.register_tree().await;
187199
}
188200
}

0 commit comments

Comments
 (0)