Bug 13430 - Crash on NSObject release when overriding RemoveFromSuperView on custom views
Summary: Crash on NSObject release when overriding RemoveFromSuperView on custom views
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.9.3.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-07-24 05:43 UTC by Simon CORSIN
Modified: 2017-05-24 16:52 UTC (History)
4 users (show)

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

Short solution that shows the crash (12.27 KB, application/zip)
2013-07-24 05:43 UTC, Simon CORSIN
Unified sample project (which works fine) (9.77 KB, application/zip)
2017-05-24 16:52 UTC, Rolf Bjarne Kvinge [MSFT]

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:

Description Simon CORSIN 2013-07-24 05:43:21 UTC
Created attachment 4409 [details]
Short solution that shows the crash

When overriding RemoveFromSuperView on custom objects from a StoryBoard/XIB, the application will randomly crash while releasing an object. It crashes totally randomly, it's not even on the same object.
Step to reproduce:
- Create a ViewController from a StoryBoard that contains a custom view
- In the custom view, just override RemoveFromSuperView (don't forget to call the base method anyway)
- Present the controller, close it, dispose it and GC.Collect()

This happens on both iOS Simulator and an actual iPhone.

I joined a showcase.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2013-07-24 08:10:40 UTC
This happens in fact because you're calling base.RemoveFromSuperview. The problem is that base.RemoveFromSuperview will access the Superview property, which is not a good idea if RemoveFromSuperview was called in the first place because the super view is being freed, in which case we'd be creating a managed wrapper for a native object that'll be freed no matter how we retain it, eventually crashing when that managed wrapper tries to release the reference it think it has.

Unfortunately I'm not sure how to fix this yet.

I haven't found any good workarounds either, but either of these should work:
* Don't override RemoveFromSuperview.
* Make sure the view that overrides RemoveFromSuperview is removed from its superview before the superview is freed. One example of this for your test code would be the following in ImageDisplayerViewController: (note that overriding ViewDidDisappear isn't necessarily the right choice for all cases, there might be other moments when it would be more appropriate, but that depends on each app)

	public override void ViewDidDisappear (bool animated)
		base.ViewDidDisappear (animated);

		this.View.Subviews[1].RemoveFromSuperview ();
Comment 2 Rolf Bjarne Kvinge [MSFT] 2017-05-24 16:50:22 UTC
I can't reproduce this anymore, after migrating the sample project to a Unified project.

There have been many memory-management related fixes, some of them may have fixed this.

I'm attaching the migrated sample, and I'll close this bug report.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2017-05-24 16:52:23 UTC
Created attachment 22429 [details]
Unified sample project (which works fine)