Swift Struct Memory Leak

前端 未结 4 1882
轻奢々
轻奢々 2021-01-30 09:05

We\'re trying to use Swift structs where we can. We are also using RxSwift which has methods which take closures. When we have a struct that creates a closure that refers to

4条回答
  •  盖世英雄少女心
    2021-01-30 09:48

    As Darren put it in the comments: "DoesItLeak can't be a struct" We cannot have the DoesItLeak be a struct and safely resolve the strong reference cycle issue.

    Value types like structs exist on the stack frame. Closures and classes are reference types.

    As the Strong Reference Cycles for Closures section puts it:

    This strong reference cycle occurs because closures, like classes, are reference types.

    Since the struct has the Variable class and the closure referring to self is stored into the Variable class using subscribeNext, it creates the strong reference cycle. See "Resolving Strong Reference Cycles for Closures" in Automatic Reference Counting Apple documentation.

提交回复
热议问题