From 3b715e97648f2af7ed4821fd6d5ec8b0e7f9f463 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 12:31:46 -0800 Subject: [PATCH] Tile: the kept pane outlines hold a weak document reference The outline cache identified its document by address, so a new document allocated at a freed document's address could show the freed document's outlines when the revision, selection and screen mapping matched. The cache now holds a Weak reference and compares it with Weak::ptr_eq, which keeps the address reserved while the outlines exist. --- crates/ui-egui/src/canvas/panes.rs | 13 +++++-------- crates/ui-egui/src/canvas/panes/tests.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/canvas/panes.rs b/crates/ui-egui/src/canvas/panes.rs index d5096bb9fb..935034e427 100644 --- a/crates/ui-egui/src/canvas/panes.rs +++ b/crates/ui-egui/src/canvas/panes.rs @@ -52,7 +52,9 @@ pub struct Panes { /// selection and screen mapping they were made for. struct Outlines { revision: u64, - doc: usize, + /// A weak reference to the document: while it exists, no other document can take the + /// allocation's address. + doc: std::sync::Weak, selection: Vec, affine: [f64; 6], shapes: Vec, @@ -61,15 +63,10 @@ struct Outlines { impl Outlines { /// Whether these outlines show `st`'s selection through the screen mapping `affine`. fn shows(&self, st: &vectorcraft_engine::DocState, affine: &[f64; 6]) -> bool { - self.revision == st.revision && self.doc == doc_address(st) && self.affine == *affine && self.selection == st.selection.objects + self.revision == st.revision && self.doc.ptr_eq(&Arc::downgrade(&st.doc)) && self.affine == *affine && self.selection == st.selection.objects } } -/// The address of `st`'s document, which tells two documents with the same revision apart. -fn doc_address(st: &vectorcraft_engine::DocState) -> usize { - Arc::as_ptr(&st.doc) as usize -} - impl Panes { /// Forget every render and pane. fn clear(&mut self) { @@ -514,7 +511,7 @@ fn selected_objects(panes: &mut Panes, slot: Slot, st: &vectorcraft_engine::DocS let affine = xf.affine().as_coeffs(); if !panes.outlines.get(&slot).is_some_and(|o| o.shows(st, &affine)) { let shapes = outline_shapes(st, xf); - let kept = Outlines { revision: st.revision, doc: doc_address(st), selection: st.selection.objects.clone(), affine, shapes }; + let kept = Outlines { revision: st.revision, doc: Arc::downgrade(&st.doc), selection: st.selection.objects.clone(), affine, shapes }; panes.outlines.insert(slot, kept); panes.outlines_made += 1; } diff --git a/crates/ui-egui/src/canvas/panes/tests.rs b/crates/ui-egui/src/canvas/panes/tests.rs index 439b19b898..d6441e08ff 100644 --- a/crates/ui-egui/src/canvas/panes/tests.rs +++ b/crates/ui-egui/src/canvas/panes/tests.rs @@ -586,6 +586,23 @@ fn in_first(app: &mut VectorcraftApp, cmd: &str, args: serde_json::Value) -> ser out } +#[test] +fn kept_outlines_do_not_show_for_a_new_document_that_takes_a_freed_documents_address() { + let affine = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + let st = vectorcraft_engine::DocState::new(vectorcraft_doc::Document::new(400.0, 300.0), None); + let kept = Outlines { revision: st.revision, doc: Arc::downgrade(&st.doc), selection: vec![], affine, shapes: vec![] }; + assert!(kept.shows(&st, &affine), "the outlines show the document they were made for"); + let freed = Arc::as_ptr(&st.doc); + drop(st); + let mut reused = false; + for _ in 0..64 { + let other = vectorcraft_engine::DocState::new(vectorcraft_doc::Document::new(400.0, 300.0), None); + reused |= Arc::as_ptr(&other.doc) == freed; + assert!(!kept.shows(&other, &affine), "a different document with the same revision, selection and mapping is not the one shown"); + } + assert!(!reused, "the weak reference keeps the freed document's address from being taken"); +} + #[test] fn other_panes_keep_their_selected_outlines_until_the_document_selection_or_view_changes() { let mut app = app_with(2);