Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 2481 [details]
See attached test case project.
DoesNotDieViewController does not get released. Instances keep increasing as user navigates back and forward until there is no more memory available.
Use Apple "Instruments" to view instances of DoesNotDieViewController. Keep navigating forward and backwards.
When using CASE 2 a release usually happens before 50 instances are live. CASE 1 never releases.
Comment or Uncomment to use either CASE 1 or CASE 2.
For case 2 it is normal that the controller isn't freed automatically, only after a while (this is how garbage collectors work).
Case 1 never releases the controller because ViewDidUnload is never executed. In this case it is completely normal that the controller isn't freed since the event handler references the controller (if it were freed you'd crash the moment the event handler were invoked).
Thanks, I should have been more clear.
Case 2 is 100% OK and works as expected.
I get what you saying about CASE 1 and yes I agree its not a bug but a misunderstanding on my part. It just feels natural to allocate in ViewDidLoad and unallocate in ViewDidUnload. When I say allocate I mean resources and references. What you saying is that all references to the ViewController and its associated View have to be dereferenced BEFORE ViewDidUnload will be called.
This is an easy mistake to make and chatting to Miguel on IRC it seems many have made it already. It would be great if MonoTouch had some way to warn the developer that ViewControllers were not being released because of some reference. The combination of ViewControllers being memory hungry and the relative ease at which this mistake can be made in my opinion is worth trying to prevent.
Thanks for your help.
iOS manages unloading itself, so it may decide to not unload views (or even not call ViewDidUnload even if a view is unloaded...), so unfortunately it is not safe to rely on ViewDidUnload for freeing resources.
We have a profiler (HeapShot) which can be used to detect memory issues, and it is also possible to use Instruments to solve these types of problems. MonoTouch doesn't know when a view is supposed to be freed (only the developer knows that), which is why we can't warn for these cases.