Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.6] Context not disposed with owner. #3450

Open
jcold opened this issue Jan 6, 2025 · 2 comments
Open

[0.6] Context not disposed with owner. #3450

jcold opened this issue Jan 6, 2025 · 2 comments

Comments

@jcold
Copy link

jcold commented Jan 6, 2025

Describe the bug

The context is not properly disposed of when the owner is disposed.

pub(crate) fn dispose_node(&self, node_id: NodeId) {

@jcold
Copy link
Author

jcold commented Jan 7, 2025

Reproduction

Main files

/// Cargo.toml

[package]
name = "leptos-context-not-disposed"
version = "0.1.0"
edition = "2021"

[profile.release]
codegen-units = 1
lto = true

[dependencies]
leptos = { version = "0.6.15", features = ["csr"] }
console_log = "1"
log = "0.4"
console_error_panic_hook = "0.1.7"

[dev-dependencies]
wasm-bindgen = "0.2"
wasm-bindgen-test = "0.3.0"
web-sys = "0.3"

/// main.rs

use std::rc::{Rc, Weak};

use leptos::*;

pub fn main() {
    _ = console_log::init_with_level(log::Level::Debug);
    console_error_panic_hook::set_once();
    mount_to_body(|| {
        view! {
            <App />
        }
    })
}

#[component]
pub fn App() -> impl IntoView {
    let opened = create_rw_signal(true);
    let rc_weak = create_rw_signal::<Option<Weak<i32>>>(None);
    let messages = create_rw_signal(vec!["Press button to destroy SubComponent".to_string()]);

    let handle_click = move |_| {
        opened.set(!opened.get());

        if !opened.get_untracked() {
            leptos::set_timeout(
                move || {
                    let rc_count = rc_weak
                        .get_untracked()
                        .map(|v| v.strong_count())
                        .unwrap_or(0);
                    messages.update(|messages| {
                        messages.push(format!("Done, Rc count: {}", rc_count));
                    });
                },
                std::time::Duration::from_secs(1),
            );
        }
    };

    view! {
        <div>
            <button on:click=handle_click>"Destroy SubComponent"</button>
            <div>
                <pre>{move || messages.get().join("\n")}</pre>
            </div>
            <Show when=move || opened.get()>
                <SubComponent rc_weak=rc_weak messages=messages />
            </Show>
        </div>
    }
}

#[component]
pub fn SubComponent(
    rc_weak: RwSignal<Option<Weak<i32>>>,
    messages: RwSignal<Vec<String>>,
) -> impl IntoView {
    let context_value = Rc::new(1);

    let value = Rc::downgrade(&context_value);
    rc_weak.set(Some(value));

    on_cleanup(move || {
        log::info!("SubComponent cleanup");
        messages.update(|messages| {
            messages.push("SubComponent cleanup".to_string());
        });
    });

    view! {
        <Provider value=context_value>
            <p>"Hello SubComponent"</p>
        </Provider>
    }
}

Steps

  1. Run trunk serve --open
  2. Press button
  3. The observed Rc strong count is 1, but the expected count is 0.

From: https://github.com/jcold/leptos-context-not-disposed

@gbj
Copy link
Collaborator

gbj commented Jan 7, 2025

It's 0 as expected on 0.7/current git main. I don't plan to work on a fix for 0.6 but would welcome a PR to the leptos_0.6 branch.

@gbj gbj changed the title Context not disposed with owner. [0.6] Context not disposed with owner. Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants