RibirX / Ribir

Non-intrusive GUI framework for Rust

Home Page:https://ribir.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

data stream memory leak if not unsubscribe correctly

M-Adoo opened this issue · comments

commented

when we convert a state to a data stream, if it is subscribed by downstream, it will cause self-reference. At this time, if the downstream does not actively unsubscribe, it will cause memory leak.

let state = State::value(0);
let c_state = state.clone();
let _h = state
  .modifies()
  // `c_state` will be stored in a `Subject` of the `state`.
  .map(move |_| *c_state.read())
  .subscribe(|v| println!("{}", v));

// if we not call `_h.unsubscribe()` in a right time, it will cause memory leak.

To solve this problem,

  1. we should provide a way to only capture the value part of the state ?
  2. or warning user need to unsubscribe and provide a method like on_dispose to call this.

We should not choose to use Weak reference, because the it is too verbose.

commented

closed by #556