fix on_id widget refcount leak (-> memory leak)
This commit is contained in:
@@ -103,7 +103,7 @@ impl Client {
|
|||||||
.width(rest(3)),
|
.width(rest(3)),
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT)
|
.span(Dir::RIGHT)
|
||||||
.add_static(&mut ui);
|
.add(&mut ui);
|
||||||
|
|
||||||
let span_test = (
|
let span_test = (
|
||||||
rrect.color(Color::GREEN).width(100),
|
rrect.color(Color::GREEN).width(100),
|
||||||
@@ -114,9 +114,10 @@ impl Client {
|
|||||||
rrect.color(Color::RED).width(100),
|
rrect.color(Color::RED).width(100),
|
||||||
)
|
)
|
||||||
.span(Dir::LEFT)
|
.span(Dir::LEFT)
|
||||||
.add_static(&mut ui);
|
.add(&mut ui);
|
||||||
|
|
||||||
let span_add = Span::empty(Dir::RIGHT).add_static(&mut ui);
|
let span_add = Span::empty(Dir::RIGHT).add(&mut ui);
|
||||||
|
let span_add_ = span_add.clone();
|
||||||
|
|
||||||
let add_button = rect(Color::LIME)
|
let add_button = rect(Color::LIME)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
@@ -125,22 +126,21 @@ impl Client {
|
|||||||
.ui
|
.ui
|
||||||
.add(image(include_bytes!("assets/sungals.png")).center())
|
.add(image(include_bytes!("assets/sungals.png")).center())
|
||||||
.any();
|
.any();
|
||||||
ctx.ui[span_add].children.push(child);
|
ctx.ui[&span_add_].children.push(child);
|
||||||
})
|
})
|
||||||
.sized((150, 150))
|
.sized((150, 150))
|
||||||
.align(Align::BOT_RIGHT);
|
.align(Align::BOT_RIGHT);
|
||||||
|
|
||||||
|
let span_add_ = span_add.clone();
|
||||||
let del_button = rect(Color::RED)
|
let del_button = rect(Color::RED)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(CursorSense::click(), move |ctx: &mut Client, _| {
|
.on(CursorSense::click(), move |ctx: &mut Client, _| {
|
||||||
ctx.ui[span_add].children.pop();
|
ctx.ui[&span_add_].children.pop();
|
||||||
})
|
})
|
||||||
.sized((150, 150))
|
.sized((150, 150))
|
||||||
.align(Align::BOT_LEFT);
|
.align(Align::BOT_LEFT);
|
||||||
|
|
||||||
let span_add_test = (span_add, add_button, del_button)
|
let span_add_test = (span_add, add_button, del_button).stack().add(&mut ui);
|
||||||
.stack()
|
|
||||||
.add_static(&mut ui);
|
|
||||||
|
|
||||||
let btext = |content| wtext(content).size(30);
|
let btext = |content| wtext(content).size(30);
|
||||||
|
|
||||||
@@ -163,10 +163,10 @@ impl Client {
|
|||||||
wtext("pretty cool right?").size(50),
|
wtext("pretty cool right?").size(50),
|
||||||
)
|
)
|
||||||
.span(Dir::DOWN)
|
.span(Dir::DOWN)
|
||||||
.add_static(&mut ui);
|
.add(&mut ui);
|
||||||
|
|
||||||
let texts = Span::empty(Dir::DOWN).gap(10).add_static(&mut ui);
|
let texts = Span::empty(Dir::DOWN).gap(10).add(&mut ui);
|
||||||
let msg_area = texts.scroll().masked().background(rect(Color::SKY));
|
let msg_area = texts.clone().scroll().masked().background(rect(Color::SKY));
|
||||||
let add_text = wtext("add")
|
let add_text = wtext("add")
|
||||||
.editable(false)
|
.editable(false)
|
||||||
.text_align(Align::LEFT)
|
.text_align(Align::LEFT)
|
||||||
@@ -183,7 +183,7 @@ impl Client {
|
|||||||
let msg_box = text
|
let msg_box = text
|
||||||
.background(rect(Color::WHITE.darker(0.5)))
|
.background(rect(Color::WHITE.darker(0.5)))
|
||||||
.add(&mut client.ui);
|
.add(&mut client.ui);
|
||||||
client.ui[texts].children.push(msg_box.any());
|
client.ui[&texts].children.push(msg_box.any());
|
||||||
})
|
})
|
||||||
.add(&mut ui);
|
.add(&mut ui);
|
||||||
let text_edit_scroll = (
|
let text_edit_scroll = (
|
||||||
@@ -207,14 +207,15 @@ impl Client {
|
|||||||
.align(Align::BOT),
|
.align(Align::BOT),
|
||||||
)
|
)
|
||||||
.span(Dir::DOWN)
|
.span(Dir::DOWN)
|
||||||
.add_static(&mut ui);
|
.add(&mut ui);
|
||||||
|
|
||||||
let main = pad_test.pad(10).add_static(&mut ui);
|
let main = pad_test.clone().pad(10).add(&mut ui);
|
||||||
|
|
||||||
let switch_button = |color, to, label| {
|
let switch_button = |color, to: WidgetId, label| {
|
||||||
|
let main_ = main.clone();
|
||||||
let rect = rect(color)
|
let rect = rect(color)
|
||||||
.id_on(CursorSense::click(), move |id, ui: &mut Ui, _| {
|
.id_on(CursorSense::click(), move |id, ui: &mut Ui, _| {
|
||||||
ui[main].inner.set_static(to);
|
ui[&main_.clone()].inner = to.clone();
|
||||||
ui[id].color = color.darker(0.3);
|
ui[id].color = color.darker(0.3);
|
||||||
})
|
})
|
||||||
.edit_on(
|
.edit_on(
|
||||||
|
|||||||
@@ -69,8 +69,10 @@ impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
|
|||||||
W::Widget: Widget,
|
W::Widget: Widget,
|
||||||
{
|
{
|
||||||
self.with_id(move |ui, id| {
|
self.with_id(move |ui, id| {
|
||||||
let id2 = id.clone();
|
// needed so that this widget can actually be dropped
|
||||||
id.on(event, move |ctx, pos| f(&id2, ctx, pos)).add(ui)
|
let id2 = id.weak();
|
||||||
|
id.on(event, move |ctx, pos| f(&id2.strong(), ctx, pos))
|
||||||
|
.add(ui)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
122
src/layout/id.rs
122
src/layout/id.rs
@@ -1,12 +1,4 @@
|
|||||||
use std::{
|
use std::{any::TypeId, marker::PhantomData, sync::mpsc::Sender};
|
||||||
any::TypeId,
|
|
||||||
marker::PhantomData,
|
|
||||||
sync::{
|
|
||||||
Arc,
|
|
||||||
atomic::{AtomicBool, Ordering},
|
|
||||||
mpsc::Sender,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{Ui, WidgetLike},
|
layout::{Ui, WidgetLike},
|
||||||
@@ -29,7 +21,15 @@ pub struct WidgetId<W = AnyWidget> {
|
|||||||
pub(super) id: Id,
|
pub(super) id: Id,
|
||||||
counter: RefCounter,
|
counter: RefCounter,
|
||||||
send: Sender<Id>,
|
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>,
|
_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> {
|
impl<W> std::fmt::Debug for WidgetId<W> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.id.fmt(f)
|
self.id.fmt(f)
|
||||||
@@ -67,20 +52,18 @@ impl<W> Clone for WidgetId<W> {
|
|||||||
ty: self.ty,
|
ty: self.ty,
|
||||||
counter: self.counter.clone(),
|
counter: self.counter.clone(),
|
||||||
send: self.send.clone(),
|
send: self.send.clone(),
|
||||||
is_static: self.is_static.clone(),
|
|
||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> WidgetId<W> {
|
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 {
|
Self {
|
||||||
ty,
|
ty,
|
||||||
id,
|
id,
|
||||||
counter: RefCounter::new(),
|
counter: RefCounter::new(),
|
||||||
send,
|
send,
|
||||||
is_static: Arc::new(is_static.into()),
|
|
||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,29 +90,47 @@ impl<W> WidgetId<W> {
|
|||||||
self.counter.refs()
|
self.counter.refs()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_static(self) -> StaticWidgetId<W> {
|
pub fn weak(&self) -> WeakWidgetId<W> {
|
||||||
self.is_static.store(true, Ordering::Release);
|
let Self {
|
||||||
StaticWidgetId {
|
ty,
|
||||||
ty: self.ty,
|
id,
|
||||||
id: self.id,
|
ref counter,
|
||||||
_pd: PhantomData,
|
ref send,
|
||||||
|
_pd,
|
||||||
|
} = *self;
|
||||||
|
WeakWidgetId {
|
||||||
|
ty,
|
||||||
|
id,
|
||||||
|
counter: counter.quiet_clone(),
|
||||||
|
send: send.clone(),
|
||||||
|
_pd,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WidgetId {
|
impl<W> WeakWidgetId<W> {
|
||||||
pub fn set_static<W>(&mut self, other: StaticWidgetId<W>) {
|
/// should guarantee that widget is still valid to prevent indexing failures
|
||||||
let send = self.send.clone();
|
pub(crate) fn strong(&self) -> WidgetId<W> {
|
||||||
drop(std::mem::replace(
|
let Self {
|
||||||
self,
|
ty,
|
||||||
Self::new(other.id, self.ty, send, true),
|
id,
|
||||||
));
|
ref counter,
|
||||||
|
ref send,
|
||||||
|
_pd,
|
||||||
|
} = *self;
|
||||||
|
WidgetId {
|
||||||
|
ty,
|
||||||
|
id,
|
||||||
|
counter: counter.clone(),
|
||||||
|
send: send.clone(),
|
||||||
|
_pd,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> Drop for WidgetId<W> {
|
impl<W> Drop for WidgetId<W> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if self.counter.drop() && !self.is_static.load(Ordering::Acquire) {
|
if self.counter.drop() {
|
||||||
let _ = self.send.send(self.id);
|
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> {
|
pub trait WidgetIdLike<W> {
|
||||||
fn id(self, send: &Sender<Id>) -> WidgetId<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> {
|
pub trait IdLike<W> {
|
||||||
fn id(&self) -> Id;
|
fn id(&self) -> Id;
|
||||||
}
|
}
|
||||||
@@ -208,9 +178,3 @@ impl<W> IdLike<W> for WidgetId<W> {
|
|||||||
self.id
|
self.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> IdLike<W> for StaticWidgetId<W> {
|
|
||||||
fn id(&self) -> Id {
|
|
||||||
self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ use image::DynamicImage;
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::{TextEdit, TextEditCtx},
|
core::{TextEdit, TextEditCtx},
|
||||||
layout::{
|
layout::{
|
||||||
Event, EventFn, EventModule, IdLike, PainterData, PixelRegion, StaticWidgetId,
|
Event, EventFn, EventModule, IdLike, PainterData, PixelRegion, TextureHandle, Vec2, Widget,
|
||||||
TextureHandle, Vec2, Widget, WidgetId, WidgetInstance, WidgetLike,
|
WidgetId, WidgetInstance, WidgetLike,
|
||||||
},
|
},
|
||||||
util::{HashSet, Id},
|
util::{HashSet, Id},
|
||||||
};
|
};
|
||||||
@@ -30,14 +30,6 @@ impl Ui {
|
|||||||
w.add(self)
|
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
|
/// useful for debugging
|
||||||
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
|
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
|
||||||
self.data.widgets.data_mut(&id.id).unwrap().label = label;
|
self.data.widgets.data_mut(&id.id).unwrap().label = label;
|
||||||
@@ -79,7 +71,6 @@ impl Ui {
|
|||||||
self.data.widgets.reserve(),
|
self.data.widgets.reserve(),
|
||||||
TypeId::of::<W>(),
|
TypeId::of::<W>(),
|
||||||
self.send.clone(),
|
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 {
|
impl dyn Widget {
|
||||||
pub fn as_any(&self) -> &dyn Any {
|
pub fn as_any(&self) -> &dyn Any {
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::WidgetPtr,
|
core::WidgetPtr,
|
||||||
layout::{Len, Painter, SizeCtx, StaticWidgetId, Ui, WidgetId, WidgetIdFn},
|
layout::{Len, Painter, SizeCtx, Ui, WidgetId, WidgetIdFn},
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::{any::Any, marker::PhantomData};
|
use std::{any::Any, marker::PhantomData};
|
||||||
@@ -26,7 +26,9 @@ pub struct FnTag;
|
|||||||
|
|
||||||
pub trait WidgetLike<Tag> {
|
pub trait WidgetLike<Tag> {
|
||||||
type Widget: 'static;
|
type Widget: 'static;
|
||||||
|
|
||||||
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
|
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
|
||||||
|
|
||||||
fn with_id<W2>(
|
fn with_id<W2>(
|
||||||
self,
|
self,
|
||||||
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
||||||
@@ -39,18 +41,14 @@ pub trait WidgetLike<Tag> {
|
|||||||
f(ui, id)
|
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)
|
fn set_root(self, ui: &mut Ui)
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
ui.set_root(self);
|
ui.set_root(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_ptr(self, ptr: &WidgetId<WidgetPtr>, ui: &mut Ui)
|
fn set_ptr(self, ptr: &WidgetId<WidgetPtr>, ui: &mut Ui)
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ impl RefCounter {
|
|||||||
let refs = self.0.fetch_sub(1, Ordering::Release);
|
let refs = self.0.fetch_sub(1, Ordering::Release);
|
||||||
refs == 0
|
refs == 0
|
||||||
}
|
}
|
||||||
|
pub fn quiet_clone(&self) -> Self {
|
||||||
|
Self(self.0.clone())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for RefCounter {
|
impl Clone for RefCounter {
|
||||||
|
|||||||
Reference in New Issue
Block a user