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.
I have a reproducable test case for you that causes a crash when the Reference counting extension is used with SGen. I'm using MT 188.8.131.52 and XamStudio 4.2.5 - all stable as of today.
Run that attached sample project in an iOS 7.1 iPhone 4 simulator (not iPad). With SGen and Reference counting extension checked run the app, tap the button to show the view, tap Cancel on the upper left. Repeat this and it will crash.
Now turn off the reference counting extension but leave SGen checked and repeat the above - no crash.
NC Software, Inc.
Created attachment 6792 [details]
That's normal (i.e. not a bug).
This happens because you are calling `AddObserver`
* without a target; and
* never calling `RemoveObserver`.
In effect the notifications are called on every `MonkeyViewController`. IOW the 2nd time you tap it's called on the first and 2nd VC (why the first ? something stills holds a reference to it and it's alive in the native side).
Now the NavigationController for the older (first) VC is not available anymore (so you get a `null` and a NullReferenceException).
You can solve this by either:
a) specifying which object instance will receive the notification, e.g.
NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillShowNotification, KeyboardShown, this);
NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, KeyboardHidden, this);
b) calling RemoveObserver (for both) when you close you VC (e.g. pass a delegate to PresentViewController completionHandler instead of `null`);
This does not happen without NRC because this is part of what NRC solves - i.e. it won't artificially keep the NavigationController alive (in a backing field) since it's now being referenced anymore.
My demo was incomplete. I checked my code in my app and saw that I was in fact calling removeObserver. I updated the demo. Can you check this out and see if this is a valid bug or something incorrect in my code again.
Created attachment 6813 [details]
Strange, it did work fine for me using RemoveObserver. I'll have a look ASAP.
So Apple notification API is not that cute (I wish it was).
You need to remove the notification based on the NSObject returned from AddObserver, e.g.
public override void ViewDidLoad()
cancelButton.Clicked += delegate
shown = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, KeyboardShown);
hidden = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, KeyboardHidden);
public override void ViewWillDisappear(bool animated)
Your sample works fine like this. It's how I originally did it (but I did not keep it that way, I modified it to use option (a) where I gave `this` to a different AddObserver overload).
Ok - thanks. Will get my code cleaned up.
There is also the prettier strongly typed notifications: