Recently I've spent some time tracking down why some objects were not getting garbage collected. I've read through various discussions on reasons why and what to do but it feels like there is variance in the recommendations. Also, the samples I've seen on github or the field service app also seem to be inconsistent. And one more factor, I'm a little unclear about how it might have changed post ios6 which seems to have affected the steps required to clean up events and/or dispose items.
Some resources on the topic include...
Discussion threads
- http://stackoverflow.com/questions/13058521/is-this-a-bug-in-monotouch-gc
- http://stackoverflow.com/questions/13050163/does-calling-releasedesigneroutlets-have-any-effect-on-monotouch-gc
- http://forums.xamarin.com/discussion/comment/535/#Comment_535
- http://monotouch.2284126.n4.nabble.com/Obsolete-methods-in-iOS-6-td4657100.html
Evolve video session on advanced mem management
Docs
- "Other Changes" section here http://docs.xamarin.com/guides/ios/platform_features/introduction_to_ios_6
Samples
- x-platform field service app: https://github.com/xamarin/prebuilt-apps/tree/master/FieldService
- monotouch samples: https://github.com/xamarin/monotouch-samples
My working conclusion at this point, is that for a normal ViewController:
- subscribe to events in ViewWillAppear
- Unsubscribe to events in ViewDidDisappear
- Dont call dispose unless you have an expensive (big/rare) resource
- due to the need to unsubscribe, lambdas must either be in a local|instance field, or use a formal method
- that's it
But it feels like there's more to the story. @Rolf or anyone want to chime in on current best practices?
Dennis