0

Why the code cause memory leak when give a new userInfo? How to fix it?

var viewModel: MineViewModel = MineViewModel(userInfo: nil)
var userInfo: UserInfo? {
    didSet {
        viewModel = MineViewModel(userInfo: userInfo)
    }
}
huangxinyu
  • 247
  • 3
  • 11
  • 1
    What is leaking? `UserInfo`? `MineViewModel`? Your view controller? Something else? The "debug memory graph" feature is really useful at identifying what's in memory and what's keeping strong reference to it. See https://stackoverflow.com/a/30993476/1271826. In answer to your question, if it's leaking, you probably have strong reference cycle somewhere, but it's not in the above code. Does your view model have closures that might have strong reference to `self`? – Rob Sep 09 '17 at 06:59
  • Hi - any reason why you need to reinitialize your MineViewModel instance rather than simply update one of its properties? – Marcus Sep 09 '17 at 13:06

0 Answers0