Bug 7008 - UIViewController not releasing when subscribing to an event on a long lived object.
Summary: UIViewController not releasing when subscribing to an event on a long lived o...
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-09-08 11:08 UTC by kevinknoop
Modified: 2012-09-10 08:58 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test Case (6.91 KB, application/zip)
2012-09-08 11:08 UTC, kevinknoop
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description kevinknoop 2012-09-08 11:08:46 UTC
Created attachment 2481 [details]
Test Case

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.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-09-10 04:32:33 UTC
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).
Comment 2 kevinknoop 2012-09-10 05:18:38 UTC
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.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2012-09-10 08:39:12 UTC
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.
Comment 4 kevinknoop 2012-09-10 08:58:41 UTC
Fair enough,

Thanks Rolf