在我们的Flutter应用程序中,内存泄漏和流没有关闭.我们将源代码追溯到以下代码:
In our Flutter application we have memory leaks and streams not being closed. We traced the source to code such as:
RxbineLatest(...).asBroadcastStream()
RxDart bineLatest()的结果是单订阅流.添加.asBroadcastStream()可使流方便地用于我们的各种Flutter显示器.但是,当这些显示器关闭时,合并的流仍处于活动状态.
The result of RxDart bineLatest() is a single-subscription stream. Adding .asBroadcastStream() makes the stream conveniently available to our various Flutter displays. However when these displays are closed the streams being combined are still active.
推荐答案来自.asBroadcastStream()文档:
返回的流将在添加第一个订阅者时订阅该流,并且将保持订阅,直到该流结束或回调取消订阅为止.
The returned stream will subscribe to this stream when its first subscriber is added, and will stay subscribed until this stream ends, or a callback cancels the subscription.
因此,根据设计,流一直存在到被明确取消为止.要在最后一个侦听器取消后取消订阅,请使用:
So by design the stream exists until explicitly cancelled. To cancel the subscription when the last listener cancels use:
RxbineLatest(...).asBroadcastStream( onCancel: (sub) => sub.cancel() )
Stream.asBroadcastStream-容易引起泄漏,这是什么?原理? #26686
更多推荐
Dart流.asBroadcastStream内存泄漏
发布评论