data stream memory leak if not unsubscribe correctly
M-Adoo opened this issue · comments
Adoo 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,
- we should provide a way to only capture the value part of the state ?
- 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.