I’m experiencing crashes when running my app on iOS5.
The code constellation I have:
- UIViewController A has an event “
InputEnded“. - UIViewController B adds A‘s view as a subview and attaches to
InputEnded. - In
InputEnded, B removes A‘s view from super view, removed the event handlers, disposes A and sets it to NULL.
In iOS 4.3 this works as expected, however A’s ViewDidDisappear() is never called, but that’s okay.
In iOS 5 however, A’s ViewDidDisappear() DOES trigger. It gets fired some point later in time. But the issue is: it fires even though A is already disposed (Handle == 0) and hence it will crash!
I both, A and B I override bool AutomaticallyForwardAppearanceAndRotationMethodsToChildViewControllers and return FALSE to keep the same behavior as I see in iOS 4.3 but appearently it will always call its own ViewDidDisappear(), no matter what you return.
Can somebody explain what is going on here and how to overcome it?
The reason why a crash occurs is that the A should not have been manually disposed, only null’ed.
Disposing A destroyed the native representation of the controller and, for iOS5 SDK, it was still required when a call to
ViewDidDisappearwas made later. The crash did not occur with iOS4.3 SDK because it did not callViewDidDisappear.So simply null’ing A means the user code has no more reference to the (managed) instance and let the GC collect it later (once nothing else has a reference to it). This works under both SDK versions and the memory is reclaimed in both case (confirmed by adding a finalizer to A to output a string to MonoDevelop’s Application Output once executed).
Full test case and explanation are available on bugzilla.