Merge pull request #2002 from ealmloff/fix-signal-leak
Fix signals leaking memory
This commit is contained in:
4 files changed
+30
-22
No files matched your search
@@ -36,18 +36,18 @@ use dioxus_signals::*;
|
|||||||
#[component]
|
#[component]
|
||||||
fn App() -> Element {
|
fn App() -> Element {
|
||||||
// Because signal is never read in this component, this component will not rerun when the signal changes
|
// Because signal is never read in this component, this component will not rerun when the signal changes
|
||||||
let signal = use_signal(|| 0);
|
let mut signal = use_signal(|| 0);
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
button {
|
button {
|
||||||
onclick: move |_| {
|
onclick: move |_| {
|
||||||
*signal.write() += 1;
|
signal += 1;
|
||||||
},
|
},
|
||||||
"Increase"
|
"Increase"
|
||||||
}
|
}
|
||||||
for id in 0..10 {
|
for id in 0..10 {
|
||||||
Child {
|
Child {
|
||||||
signal: signal,
|
signal,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,11 +58,10 @@ struct ChildProps {
|
|||||||
signal: Signal<usize>,
|
signal: Signal<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
fn Child(props: ChildProps) -> Element {
|
||||||
fn Child(cx: Scope<ChildProps>) -> Element {
|
|
||||||
// This component does read from the signal, so when the signal changes it will rerun
|
// This component does read from the signal, so when the signal changes it will rerun
|
||||||
rsx! {
|
rsx! {
|
||||||
"{cx.props.signal}"
|
"{props.signal}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -85,7 +84,7 @@ fn App() -> Element {
|
|||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn Child() -> Element {
|
fn Child() -> Element {
|
||||||
let signal: Signal<i32> = *use_context(cx).unwrap();
|
let signal: Signal<i32> = use_context();
|
||||||
// This component does read from the signal, so when the signal changes it will rerun
|
// This component does read from the signal, so when the signal changes it will rerun
|
||||||
rsx! {
|
rsx! {
|
||||||
"{signal}"
|
"{signal}"
|
||||||
@@ -105,12 +104,12 @@ use dioxus_signals::*;
|
|||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn App() -> Element {
|
fn App() -> Element {
|
||||||
let signal = use_signal(|| 0);
|
let mut signal = use_signal(|| 0);
|
||||||
let doubled = use_memo(|| signal * 2);
|
let doubled = use_memo(move || signal * 2);
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
button {
|
button {
|
||||||
onclick: move |_| *signal.write() += 1,
|
onclick: move |_| signal += 1,
|
||||||
"Increase"
|
"Increase"
|
||||||
}
|
}
|
||||||
Child {
|
Child {
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ fn current_owner<S: Storage<T>, T>() -> Owner<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn owner_in_scope<S: Storage<T>, T>(scope: ScopeId) -> Owner<S> {
|
fn owner_in_scope<S: Storage<T>, T>(scope: ScopeId) -> Owner<S> {
|
||||||
match consume_context_from_scope(scope) {
|
match scope.has_context() {
|
||||||
Some(rt) => rt,
|
Some(rt) => rt,
|
||||||
None => {
|
None => {
|
||||||
let owner = S::owner();
|
let owner = S::owner();
|
||||||
|
|||||||
@@ -30,17 +30,15 @@ use std::{
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// #[component]
|
/// #[component]
|
||||||
/// fn Child(state: Signal<u32>) -> Element {
|
/// fn Child(mut state: Signal<u32>) -> Element {
|
||||||
/// let state = *state;
|
/// use_future(move || async move {
|
||||||
///
|
|
||||||
/// use_future( |()| async move {
|
|
||||||
/// // Because the signal is a Copy type, we can use it in an async block without cloning it.
|
/// // Because the signal is a Copy type, we can use it in an async block without cloning it.
|
||||||
/// *state.write() += 1;
|
/// state += 1;
|
||||||
/// });
|
/// });
|
||||||
///
|
///
|
||||||
/// rsx! {
|
/// rsx! {
|
||||||
/// button {
|
/// button {
|
||||||
/// onclick: move |_| *state.write() += 1,
|
/// onclick: move |_| state += 1,
|
||||||
/// "{state}"
|
/// "{state}"
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
|
|||||||
@@ -56,6 +56,11 @@ fn deref_signal() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn drop_signals() {
|
fn drop_signals() {
|
||||||
|
use std::sync::atomic::AtomicUsize;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
|
||||||
|
static SIGNAL_DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
let mut dom = VirtualDom::new(|| {
|
let mut dom = VirtualDom::new(|| {
|
||||||
let generation = generation();
|
let generation = generation();
|
||||||
|
|
||||||
@@ -68,10 +73,18 @@ fn drop_signals() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
fn Child() -> Element {
|
fn Child() -> Element {
|
||||||
let signal = create_without_cx();
|
struct TracksDrops;
|
||||||
|
|
||||||
|
impl Drop for TracksDrops {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
SIGNAL_DROP_COUNT.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use_signal(|| TracksDrops);
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
"{signal}"
|
""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +92,5 @@ fn drop_signals() {
|
|||||||
dom.mark_dirty(ScopeId::ROOT);
|
dom.mark_dirty(ScopeId::ROOT);
|
||||||
dom.render_immediate(&mut NoOpMutations);
|
dom.render_immediate(&mut NoOpMutations);
|
||||||
|
|
||||||
fn create_without_cx() -> Signal<String> {
|
assert_eq!(SIGNAL_DROP_COUNT.load(Ordering::Relaxed), 10);
|
||||||
Signal::new("hello world".to_string())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user