fix on_id widget refcount leak (-> memory leak)

This commit is contained in:
2025-12-04 02:59:05 -05:00
parent 84c460a91f
commit e5d0a7e592
6 changed files with 74 additions and 130 deletions

View File

@@ -69,8 +69,10 @@ impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
W::Widget: Widget,
{
self.with_id(move |ui, id| {
let id2 = id.clone();
id.on(event, move |ctx, pos| f(&id2, ctx, pos)).add(ui)
// needed so that this widget can actually be dropped
let id2 = id.weak();
id.on(event, move |ctx, pos| f(&id2.strong(), ctx, pos))
.add(ui)
})
}

View File

@@ -1,12 +1,4 @@
use std::{
any::TypeId,
marker::PhantomData,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
mpsc::Sender,
},
};
use std::{any::TypeId, marker::PhantomData, sync::mpsc::Sender};
use crate::{
layout::{Ui, WidgetLike},
@@ -29,7 +21,15 @@ pub struct WidgetId<W = AnyWidget> {
pub(super) id: Id,
counter: RefCounter,
send: Sender<Id>,
is_static: Arc<AtomicBool>,
_pd: PhantomData<W>,
}
#[repr(C)]
pub struct WeakWidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
pub(super) id: Id,
counter: RefCounter,
send: Sender<Id>,
_pd: PhantomData<W>,
}
@@ -39,21 +39,6 @@ impl<W> PartialEq for WidgetId<W> {
}
}
/// A WidgetId for a static widget that cannot be removed from a Ui.
/// Useful because ergonomic clones don't exist yet so you can easily use these in closures.
/// Do not use this if you want the widget to be freeable.
///
/// This is currently not perfectly efficient and just creates new WidgetIds every time it's used,
/// but they don't send drop messages to Ui.
/// Ideally I'd have an enum or something that lets you use either, but that doesn't seem worth it
/// right now; it's good enough and relatively cheap.
#[repr(C)]
pub struct StaticWidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
pub(super) id: Id,
_pd: PhantomData<W>,
}
impl<W> std::fmt::Debug for WidgetId<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.id.fmt(f)
@@ -67,20 +52,18 @@ impl<W> Clone for WidgetId<W> {
ty: self.ty,
counter: self.counter.clone(),
send: self.send.clone(),
is_static: self.is_static.clone(),
_pd: PhantomData,
}
}
}
impl<W> WidgetId<W> {
pub(super) fn new(id: Id, ty: TypeId, send: Sender<Id>, is_static: bool) -> Self {
pub(super) fn new(id: Id, ty: TypeId, send: Sender<Id>) -> Self {
Self {
ty,
id,
counter: RefCounter::new(),
send,
is_static: Arc::new(is_static.into()),
_pd: PhantomData,
}
}
@@ -107,29 +90,47 @@ impl<W> WidgetId<W> {
self.counter.refs()
}
pub fn into_static(self) -> StaticWidgetId<W> {
self.is_static.store(true, Ordering::Release);
StaticWidgetId {
ty: self.ty,
id: self.id,
_pd: PhantomData,
pub fn weak(&self) -> WeakWidgetId<W> {
let Self {
ty,
id,
ref counter,
ref send,
_pd,
} = *self;
WeakWidgetId {
ty,
id,
counter: counter.quiet_clone(),
send: send.clone(),
_pd,
}
}
}
impl WidgetId {
pub fn set_static<W>(&mut self, other: StaticWidgetId<W>) {
let send = self.send.clone();
drop(std::mem::replace(
self,
Self::new(other.id, self.ty, send, true),
));
impl<W> WeakWidgetId<W> {
/// should guarantee that widget is still valid to prevent indexing failures
pub(crate) fn strong(&self) -> WidgetId<W> {
let Self {
ty,
id,
ref counter,
ref send,
_pd,
} = *self;
WidgetId {
ty,
id,
counter: counter.clone(),
send: send.clone(),
_pd,
}
}
}
impl<W> Drop for WidgetId<W> {
fn drop(&mut self) {
if self.counter.drop() && !self.is_static.load(Ordering::Acquire) {
if self.counter.drop() {
let _ = self.send.send(self.id);
}
}
@@ -158,31 +159,6 @@ impl<W: 'static, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike<IdFnTag> for F {
}
}
impl<W> StaticWidgetId<W> {
pub fn to_id(&self, send: &Sender<Id>) -> WidgetId<W> {
WidgetId::new(self.id, self.ty, send.clone(), true)
}
pub fn any(self) -> StaticWidgetId<AnyWidget> {
// SAFETY: self is repr(C)
unsafe { std::mem::transmute(self) }
}
}
impl<W: 'static> WidgetLike<IdTag> for StaticWidgetId<W> {
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> {
self.id(&ui.send)
}
}
impl<W> Clone for StaticWidgetId<W> {
fn clone(&self) -> Self {
*self
}
}
impl<W> Copy for StaticWidgetId<W> {}
pub trait WidgetIdLike<W> {
fn id(self, send: &Sender<Id>) -> WidgetId<W>;
}
@@ -193,12 +169,6 @@ impl<W> WidgetIdLike<W> for &WidgetId<W> {
}
}
impl<W> WidgetIdLike<W> for StaticWidgetId<W> {
fn id(self, send: &Sender<Id>) -> WidgetId<W> {
self.to_id(send)
}
}
pub trait IdLike<W> {
fn id(&self) -> Id;
}
@@ -208,9 +178,3 @@ impl<W> IdLike<W> for WidgetId<W> {
self.id
}
}
impl<W> IdLike<W> for StaticWidgetId<W> {
fn id(&self) -> Id {
self.id
}
}

View File

@@ -3,8 +3,8 @@ use image::DynamicImage;
use crate::{
core::{TextEdit, TextEditCtx},
layout::{
Event, EventFn, EventModule, IdLike, PainterData, PixelRegion, StaticWidgetId,
TextureHandle, Vec2, Widget, WidgetId, WidgetInstance, WidgetLike,
Event, EventFn, EventModule, IdLike, PainterData, PixelRegion, TextureHandle, Vec2, Widget,
WidgetId, WidgetInstance, WidgetLike,
},
util::{HashSet, Id},
};
@@ -30,14 +30,6 @@ impl Ui {
w.add(self)
}
pub fn add_static<W: Widget, Tag>(
&mut self,
w: impl WidgetLike<Tag, Widget = W>,
) -> StaticWidgetId<W> {
let id = w.add(self);
id.into_static()
}
/// useful for debugging
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
self.data.widgets.data_mut(&id.id).unwrap().label = label;
@@ -79,7 +71,6 @@ impl Ui {
self.data.widgets.reserve(),
TypeId::of::<W>(),
self.send.clone(),
false,
)
}
@@ -215,21 +206,6 @@ impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
}
}
impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
self.data.widgets.get(&id).unwrap()
}
}
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.insert(id.id);
self.data.widgets.get_mut(&id).unwrap()
}
}
impl dyn Widget {
pub fn as_any(&self) -> &dyn Any {
self

View File

@@ -1,6 +1,6 @@
use crate::{
core::WidgetPtr,
layout::{Len, Painter, SizeCtx, StaticWidgetId, Ui, WidgetId, WidgetIdFn},
layout::{Len, Painter, SizeCtx, Ui, WidgetId, WidgetIdFn},
};
use std::{any::Any, marker::PhantomData};
@@ -26,7 +26,9 @@ pub struct FnTag;
pub trait WidgetLike<Tag> {
type Widget: 'static;
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
@@ -39,18 +41,14 @@ pub trait WidgetLike<Tag> {
f(ui, id)
}
}
fn add_static(self, ui: &mut Ui) -> StaticWidgetId<Self::Widget>
where
Self: Sized,
{
self.add(ui).into_static()
}
fn set_root(self, ui: &mut Ui)
where
Self: Sized,
{
ui.set_root(self);
}
fn set_ptr(self, ptr: &WidgetId<WidgetPtr>, ui: &mut Ui)
where
Self: Sized,