Bug 26517 - NSWindow.DefaultButtonCell throws when set to null
Summary: NSWindow.DefaultButtonCell throws when set to null
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 1.11.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 2.0.x
Assignee: Chris Hamons
Depends on:
Reported: 2015-01-28 16:13 UTC by Michael Teper
Modified: 2015-03-27 08:28 UTC (History)
6 users (show)

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

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 Michael Teper 2015-01-28 16:13:08 UTC
I need a way to make a default button not default, and I can find no way to do so with Xam.Mac. This is a blocking issue for our interface right now.
Comment 1 Michael Teper 2015-01-28 17:14:27 UTC
For reference, this code works fine:

var window = NSApplication.SharedApplication.Windows[0].Handle;
var selector = Selector.GetHandle("setDefaultButtonCell:");
Messaging.IntPtr_objc_msgSend_IntPtr(window, selector, IntPtr.Zero);

I have to ask, given this is not my first "throws when set to null" bug, why the over-aggressive throwing of null ref exceptions?!
Comment 2 Chris Hamons 2015-02-04 11:06:17 UTC
@Michael - If you are blocked by an issue and have a business license, as it appears you do, _please_ please use:


instead of filing a raw bug. We use that to fast-lane looking into issues blocking customers like yourself.

To answer your question, this is a binding bug. The objective-c Cocoa API does not give sufficient information to let us know when setting null is allowed or not. We try to catch and test these cases when writing bindings, but we fail-safe to throw by default. This is because catching a C# exception is much more pleasant than an NSException / Native crash.

We'll look at fixing this binding mistake in a future version of Xamarin.Mac, but your work around looks solid until then.
Comment 6 Chris Hamons 2015-02-06 10:13:19 UTC
If you wish to turn off the "safety" and remove these thread checks, set the following in your projects:

NSApplication.CheckForIllegalCrossThreadCalls = false;

This will disable all 

Comment 7 Michael Teper 2015-02-07 16:03:13 UTC
Would this disable both cross-thread exceptions (which are very useful!) and null red checks?
Comment 8 Chris Hamons 2015-02-09 08:56:13 UTC
Ignore that. (It's what I get for trying to reply to bugs without sufficient coffee.) We have a way of disabling null checks, but it is at compile time (on our end) not run time. I misread the code.

It looks like we may have looked into the ability to disable null checks at runtime, but it  increased the cost of the checks in the "normal" case (extra static boolean lookup for every single function call that passes params).
Comment 9 Michael Teper 2015-02-16 00:23:04 UTC
Chris, I attempted to convert our app to Xamarin.Mac (Unified). Took some cajoling to get the application to build, but this API snafu is now a much bigger issue because with Unified I no longer have the luxury of dropping down to lower level calls (see https://bugzilla.xamarin.com/show_bug.cgi?id=25847).
Comment 10 Timothy Risi 2015-02-20 21:42:25 UTC
Fixed on master / 714ee6b4dd94e029de6d09a83a79c56609b6e391
Comment 13 Rajneesh Kumar 2015-03-27 08:28:31 UTC
@ Chris, thanks a lot for sharing the steps to check this issue.

I have checked this issue with the following builds:

=== Xamarin Studio ===
Version 5.9 (build 310)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Mono 4.0.0 ((detached/87c62a1)
GTK+ 2.24.23 (Raleigh theme)
Package version: 400000116
Apple Developer Tools Xcode 6.2 (6776)
Build 6C131e
Xamarin.Mac Version: (Business Edition)
=== Build Information ===
Release ID: 509000310
Git revision: 5e63e3a7face15bd906106306fb4565eaf503995
Build date: 2015-03-26 12:30:38-04
Xamarin addins: 14cabb8b8f6b94fb7aac79016d64f4b9db4d0091
Operating System Mac OS X 10.9.5
Darwin MacMini.local 13.4.0 Darwin Kernel Version 13.4.0
Sun Aug 17 19:50:11 PDT 2014
root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64

I observed that this issue is working fine. Now, NSWindow.DefaultButtonCell does't throws exception when set to null. 
Here is the screencast for the same: http://www.screencast.com/t/4t2jl1GIrm7

This issue has been fixed, hence I am closing this issue.