C++: Fix crash when deleting a component from the click event within a sub component

The parent is already deleted when we delete the component that is kept
alive by the `grabber` ItemRc in `handle_mouse_grab`
When the ItemRc goes out of scope, we delete the component, but the
parent was already gone.

This doesn't solve a broader issue that we use the parent in many place
and it is not kept alive (for example when using properties of the
parent)
This commit is contained in:
Olivier Goffart 2023-09-14 10:44:24 +02:00 committed by Olivier Goffart
parent 493f3bd236
commit f546d22861
2 changed files with 86 additions and 8 deletions

View file

@ -1060,8 +1060,6 @@ fn generate_item_tree(
conditional_includes,
);
let root_access = if parent_ctx.is_some() { "parent->root" } else { "self" };
let mut item_tree_array: Vec<String> = Default::default();
let mut item_array: Vec<String> = Default::default();
@ -1411,6 +1409,7 @@ fn generate_item_tree(
create_code.push("slint::cbindgen_private::slint_ensure_backend();".into());
}
let root_access = if parent_ctx.is_some() { "parent->root" } else { "self" };
create_code.extend([
format!(
"slint::private_api::register_component(&self_rc.into_dyn(), {root_access}->m_window);",
@ -1442,12 +1441,10 @@ fn generate_item_tree(
}),
));
let mut destructor = vec!["auto self = this;".to_owned()];
destructor.push(format!(
"if (auto &window = {}->m_window) window->window_handle().unregister_component(self, item_array());",
root_access
));
let root_access = if parent_ctx.is_some() { "root" } else { "this" };
let destructor = vec![format!(
"if (auto &window = {root_access}->m_window) window->window_handle().unregister_component(this, item_array());"
)];
target_struct.members.push((
Access::Public,