Here the Swift class that uses ReactiveSwift wraps MutablePropertyin Propertyand adds a subscription to Propertyin ScopedDisposable:
class Leaker {
let mutableProperty = MutableProperty<Int>(0)
var wrapperProperty: Property<Int> {
return Property(self.mutableProperty)
}
private var disposable: ScopedDisposable<AnyDisposable>?
init() {
let disposable = CompositeDisposable()
disposable += self.wrapperProperty.producer
.startWithValues { value in
print("\(value)")
}
self.disposable = ScopedDisposable(disposable)
}
}
If I give another class a type property Leaker?and then set it with self.leaker = Leaker(), this creates a leak. Creates a leak, I mean that it installs the Leaks tool, showing a leaked object with a label Malloc 32 Bytes, with a stack trace that includes the Leaker.init()call Leaker.wrapperProperty.getter.
Why is this leak? It’s hard for me to understand what exactly makes the memory allocated here never be released.
Some other facts that may be helpful:
- This is not a leak if I sign up
MutablePropertydirectly - ,
MutableProperty - ,
Leaker, . let _ = Leaker()