Bug 13073 - UIView created by UIViewController does not have a reference to the ViewController, resulting in SEGFAULT
Summary: UIView created by UIViewController does not have a reference to the ViewContr...
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.9.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-07-05 14:11 UTC by Ivo Smits
Modified: 2013-07-05 14:45 UTC (History)
2 users (show)

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


Attachments
Test case project (12.91 KB, application/zip)
2013-07-05 14:11 UTC, Ivo Smits
Details
Stack trace (3.40 KB, text/plain)
2013-07-05 14:12 UTC, Ivo Smits
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 FEATURE

Description Ivo Smits 2013-07-05 14:11:13 UTC
Created attachment 4277 [details]
Test case project

When an UIView is created by a designer-generated UIViewController and used as a child view like this:
  ViewController2 child = new ViewController2();
  View.Add(child.View);

It looks like no reference to 'child' is stored, which results in a segmentation fault when the child view calls an event handler on its ViewController.

Explicitly storing a reference to the ViewController instance prevents the crash, as shown in the test case project, in file ViewController1.cs. So far I have only seen this crash happening in the iOS simulator.

The stack trace is included in the test case project archive, named Stacktrace.txt.
Comment 1 Ivo Smits 2013-07-05 14:12:02 UTC
Created attachment 4278 [details]
Stack trace
Comment 2 Sebastien Pouliot 2013-07-05 14:45:36 UTC
The C# code above does not maintain a managed reference to `child`. That means that the GC is free to collect the managed instance at anytime.

However the native peer to the controller still exists (not an issue in itself) and could try to call back (e.g. events) into managed code (that's an issue since it was collected). To avoid this you need to keep a reference (e.g. a field) to the UIViewController as long as you're using its View. 

Here's a good video on the subject from the Evolve conference: http://xamarin.com/evolve/2013#session-0w86u7bco2

> So far I have only seen this crash happening in the iOS simulator.

That's normal. The GC is configured to collect very frequently on the simulator (that's sometime referred, by some people, as being "aggressive"). This is by design since:

(a) there's a lot of CPU available on your Mac;

(b) it make it easier (and faster) to find such mistakes (that could be very difficult to reproduce otherwise). IOW it will happen on device but it's easy to miss (while testing) and you do not want your customers to find those before you do.