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 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.
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)
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.
Created attachment 22429 [details]
Unified sample project (which works fine)