Bug 22909 - NSButton Highlight with subclassed NSView with overrideProperty Frame Fails
Summary: NSButton Highlight with subclassed NSView with overrideProperty Frame Fails
Status: CONFIRMED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Other ()
Version: 1.10.0
Hardware: PC Mac OS
: Low normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-09-12 05:47 UTC by David Lilley
Modified: 2016-09-26 21:58 UTC (History)
9 users (show)

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


Attachments
Button Highlight Test Fail (23.99 KB, application/zip)
2014-09-12 05:47 UTC, David Lilley
Details
Screenshot of all threads and call stack (227.80 KB, image/png)
2014-09-12 10:24 UTC, David Lilley
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 for Bug 22909 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description David Lilley 2014-09-12 05:47:21 UTC
Created attachment 8018 [details]
Button Highlight Test Fail

The following situation fails.

I have NSbutton that is disabled on a subclassed NSView with overridden property Frame.
the NSButton has KeyEquivalent = "\r";

Expected
When the button is enabled I would expect the flashing button to show ready for pressing enter..

Result
Crash.

Unhandled Exception:
MonoMac.AppKit.AppKitThreadAccessException: AppKit Consistency error: you are calling a method that can only be invoked from the UI thread.
at MonoMac.AppKit.NSApplication.EnsureUIThread () [0x0001a] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/AppKit/NSApplication.cs:111
at MonoMac.AppKit.NSView.get_Frame () [0x00001] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/build/compat/AppKit/NSView.g.cs:3548
at Subsembly.Util.View.get_Frame () [0x00002] in /Users/dlilley/Projects/TestArchiver/TestArchiver/View.cs:71
at (wrapper dynamic-method) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x0000b, 0x00054>
at (wrapper native-to-managed) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x00050, 0x0011f>

[ERROR] FATAL UNHANDLED EXCEPTION: MonoMac.AppKit.AppKitThreadAccessException: AppKit Consistency error: you are calling a method that can only be invoked from the UI thread.
at MonoMac.AppKit.NSApplication.EnsureUIThread () [0x0001a] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/AppKit/NSApplication.cs:111
at MonoMac.AppKit.NSView.get_Frame () [0x00001] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/build/compat/AppKit/NSView.g.cs:3548
at Subsembly.Util.View.get_Frame () [0x00002] in /Users/dlilley/Projects/TestArchiver/TestArchiver/View.cs:71
at (wrapper dynamic-method) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x0000b, 0x00054>
at (wrapper native-to-managed) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x00050, 0x0011f>
Comment 1 David Lilley 2014-09-12 05:47:45 UTC
Xamarin Studio
Version 5.3 (build 441)
Installation UUID: 7d614215-ae86-4132-8cff-53347f7978db
Runtime:
	Mono 3.8.0 ((no/45d0ba1)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000009

Apple Developer Tools
Xcode 5.0.2 (3335.32)
Build 5A3005

Xamarin.iOS
Version: 7.4.0.108 (Business Edition)
Hash: 77efa3f
Branch: 
Build date: 2014-08-26 07:59:55-0400

Xamarin.Android
Not Installed

Xamarin.Mac
Version: 1.10.0.10 (Business Edition)

Build Information
Release ID: 503000441
Git revision: befb6aa1176d37a5f678f4274f340a0159091b7a
Build date: 2014-09-08 17:57:02-04
Xamarin addins: 6dc7c388e31fdfc8014689839d37de0d4622435c

Operating System
Mac OS X 10.9.4
Darwin Dave-Lilleys-iMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 2 David Lilley 2014-09-12 05:48:11 UTC
Also if you remove Frame Property it works!
Comment 3 Arpit Jha 2014-09-12 06:16:11 UTC
I have checked this issue and able to reproduce it.

I have tried to reproduce it with the help of bug description.

I observed that when I clicked on button and getting an Exception "MonoMac.AppKit.AppKitThreadAccessException" but when I Comment following code
ButtonHighlightTest -> View.cs

 public View( RectangleF aFrame ) : base(aFrame)
        {

        }

      
        /// <summary>
        /// Gets or sets the frame.
        /// </summary>
        /// <value>The frame.</value>
        public override RectangleF Frame
        {
            get
            {
                return base.Frame;
   
            }
            set
            {
                base.Frame = value;
              
            }
        }

then it works fine as expected.

Screencast regarding Same : http://screencast.com/t/LqPPXLBf6

Environment Info :
=== Xamarin Studio ===

Version 5.3 (build 441)
Installation UUID: 2591d519-875d-4afe-a3d9-5fcf391bbd2d
Runtime:
 Mono 3.8.0 ((no/45d0ba1)
 GTK+ 2.24.23 (Raleigh theme)

 Package version: 308000009

=== Apple Developer Tools ===

Xcode 4.6.3 (2068)
Build 4H1503

=== Xamarin.Mac ===

Version: 1.10.0.10 (Enterprise Edition)

=== Xamarin.iOS ===

Version: 7.4.0.108 (Enterprise Edition)
Hash: 77efa3f
Branch: 
Build date: 2014-08-26 07:59:55-0400

=== Xamarin.Android ===

Version: 4.17.0 (Enterprise Edition)
Android SDK: /Users/nischal/Desktop/android-sdk-macosx
 Supported Android versions:
  2.1    (API level 7)
  2.2    (API level 8)
  2.3    (API level 10)
  3.1    (API level 12)
  3.2    (API level 13)
  4.0    (API level 14)
  4.0.3  (API level 15)
  4.1    (API level 16)
  4.2    (API level 17)
  4.3    (API level 18)
  4.4    (API level 19)
  4.4.87 (API level 20)
  4.5    (API level 21)
Java SDK: /usr
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

=== Build Information ===

Release ID: 503000441
Git revision: befb6aa1176d37a5f678f4274f340a0159091b7a
Build date: 2014-09-08 17:57:02-04
Xamarin addins: 6dc7c388e31fdfc8014689839d37de0d4622435c

=== Operating System ===

Mac OS X 10.7.4
Darwin nischals-Mac-mini.local 11.4.0 Darwin Kernel Version 11.4.0
    Mon Apr  9 19:32:15 PDT 2012
    root:xnu-1699.26.8~1/RELEASE_X86_64 x86_64
Comment 4 David Lilley 2014-09-12 07:21:40 UTC
Properties which also fail.. 
DrawRect
BorderColor
Comment 5 Chris Hamons 2014-09-12 09:15:57 UTC
MonoMac.AppKit.AppKitThreadAccessException: AppKit Consistency error: you are
calling a method that can only be invoked from the UI thread.
at MonoMac.AppKit.NSApplication.EnsureUIThread () [0x0001a] in

This means you are not on the main thread and you are attempting to access UI elements.

You need to invoke back to the main thread using something like: NSApplication.SharedApplication.BeginInvokeOnMainThread ()

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

Main Thread Only Classes

The following classes must be used only from the main thread of an application.

NSCell and all of its descendants
NSView and all of its descendants. For more information, see “NSView Restrictions.”

http://developer.xamarin.com/guides/ios/user_interface/controls/part_2_-_working_with_the_ui_thread/
Comment 6 David Lilley 2014-09-12 09:32:45 UTC
Have you seen my code ? its a small sample please check. As I understand.

the place it could jump on the wrong thread is... which i believe is correct.

            aButton.Activated += delegate
            {
                aHighlightbutton.Enabled = true;
            };


otherwise I need to protect View.frame property invoke on the main thread which I think is wrong ?
Comment 7 Chris Hamons 2014-09-12 09:58:04 UTC
Yeah, I only glanced at the sample code, my mistake. I was mostly looking at the stack trace. (I blame the lack of caffeine this morning, apologies).

I am unable to reproduce on my machine, OS X 10.10. I don't have access to a 10.7 machine at the moment, but I have bits downloading on my 10.8 VM.

Can you post the stack trace of the crash with the example, the original post here appears to be for another example and/or is shortened.
Comment 8 David Lilley 2014-09-12 10:03:31 UTC
Sure... I am thinking I have one to many coffees today...perhaps its beer o'clock

Unhandled Exception:
MonoMac.AppKit.AppKitThreadAccessException: AppKit Consistency error: you are calling a method that can only be invoked from the UI thread.
at MonoMac.AppKit.NSApplication.EnsureUIThread () [0x0001a] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/AppKit/NSApplication.cs:111
at MonoMac.AppKit.NSView.get_Frame () [0x00001] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/build/compat/AppKit/NSView.g.cs:3548
at Subsembly.Util.View.get_Frame () [0x00002] in /Users/dlilley/Projects/ButtonHighlightTest/TestArchiver/View.cs:67
at (wrapper dynamic-method) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x0000b, 0x00054>
at (wrapper native-to-managed) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x00050, 0x0011f>

[ERROR] FATAL UNHANDLED EXCEPTION: MonoMac.AppKit.AppKitThreadAccessException: AppKit Consistency error: you are calling a method that can only be invoked from the UI thread.
at MonoMac.AppKit.NSApplication.EnsureUIThread () [0x0001a] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/AppKit/NSApplication.cs:111
at MonoMac.AppKit.NSView.get_Frame () [0x00001] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/1bfb1eb7/source/xamcore/src/build/compat/AppKit/NSView.g.cs:3548
at Subsembly.Util.View.get_Frame () [0x00002] in /Users/dlilley/Projects/ButtonHighlightTest/TestArchiver/View.cs:67
at (wrapper dynamic-method) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x0000b, 0x00054>
at (wrapper native-to-managed) object.[Subsembly.Util.View.System.Drawing.RectangleF get_Frame()] (intptr,MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x00050, 0x0011f>
Comment 9 David Lilley 2014-09-12 10:04:47 UTC
Sorry thats the full stack trace!
Comment 10 Chris Hamons 2014-09-12 10:16:41 UTC
Hmm, that's all XS shows when you open up the Call Stack pad? That doesn't seem to show the thread creation..

Still downloading onto my VM btw.
Comment 11 David Lilley 2014-09-12 10:24:53 UTC
Created attachment 8031 [details]
Screenshot of all threads and call stack
Comment 12 David Lilley 2014-09-12 10:28:42 UTC
Ps I think it might be animation thread, as if you remove  KeyEquivalent = "\r"; it works fine.
Comment 13 David Lilley 2014-09-15 04:03:56 UTC
Chris, I noticed this bug still stays Need Info... What more info do you need ?
Comment 14 Chris Hamons 2014-09-15 09:44:11 UTC
I was waiting to confirm the issue locally before removing.

My 10.7 VM shows the same behavior. That makes me think that Apple changed how this works (to invoke to the main thread) sometime between 10.7 and 10.10.

As a workaround, removing KeyEquivalent does seem to work for me.

I'll talk to my team about removing the UI check for frame to see if that is a good idea.
Comment 15 David Lilley 2014-09-15 09:57:05 UTC
I have not tested it with all the properties but I have also experienced it with 

DrawRect
BorderColor

So I expect it could be a lot of properties that would need ignoring ...
Comment 16 Rolf Bjarne Kvinge [MSFT] 2014-12-19 02:12:51 UTC
Another workaround is to set NSApplication.CheckForIllegalCrossThreadCalls = false.
Comment 17 Chris Hamons 2014-12-19 12:35:48 UTC
This is fixed in master/861334b2c4beafcd746381f52c28c1802a6dcbd6.

Please report any additional APIs you run into that have that issue.
Comment 18 Akhilesh kumar 2015-01-20 11:05:51 UTC
I have checked this issue and observing that we are still getting same issue on OXS 10.9.5, 10.8.4 however it is working fine on OSX 10.10.1

Screencast(Yosemite/ML): http://www.screencast.com/t/iSyOEfu9x
Screencast(Mavericks):http://www.screencast.com/t/iGtkE0BKAMsI

As of now I am reopening this issue.

Please let me know if the test project work only for OSX 10.10.1 ?

Environment Info:

=== Xamarin Studio ===

Version 5.7.1 (build 14)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Runtime:
 Mono 3.12.0 ((detached/a813491)
 GTK+ 2.24.23 (Raleigh theme)

 Package version: 312000068

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/MM/Desktop/android-sdk-macosx
 Supported Android versions:
  2.1    (API level 7)
  2.2    (API level 8)
  2.3    (API level 10)
  3.1    (API level 12)
  4.0    (API level 14)
  4.0.3  (API level 15)
  4.1    (API level 16)
  4.2    (API level 17)
  4.3    (API level 18)
  4.4    (API level 19)
  4.4.87 (API level 20)
  5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.iOS ===

Version: 8.6.1.6 (Business Edition)
Hash: dd1b996
Branch: 
Build date: 2015-01-15 15:06:20-0500

=== Xamarin.Mac ===

Version: 1.12.0.4 (Business Edition)

=== Build Information ===

Release ID: 507010014
Git revision: a4dd61ad7f8b3695be4b17bcb5c3ae6b81438cf7
Build date: 2015-01-19 15:21:09-05
Xamarin addins: 081208fe3bbf40e24a562867c6c7fba20a9b94b6

=== 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
[9:34:26 PM] Rajneesh Kumar: 22909
Comment 19 Chris Hamons 2015-02-18 17:17:03 UTC
Sorry, the milestone was incorrect. This is going to make 2.0 (master) not 1.12.
Comment 20 Parmendra Kumar 2015-03-02 08:47:10 UTC
I have checked this issue with Xam.Mac Version: 1.13.1.397 and I am still getting same behavior mentioned in the comment #18 as Its working fine with OSX 10.10.1 and still getting Exception with OSX 10.9.4

Please let me know if this test project work only for OSX 10.10.1 ?

Environment info:

=== Xamarin Studio ===

Version 5.8 (build 1033)
Installation UUID: 1a096c6f-0678-402e-89b2-a2c10f7e80e4
Runtime:
	Mono 4.0.0 ((detached/2634099)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400000017

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.9.1.397 (Business Edition)
Hash: 016fd4e
Branch: master
Build date: 2015-02-26 19:15:06-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/360_macmini/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 1.13.1.397 (Business Edition)

=== Build Information ===

Release ID: 508001033
Git revision: e8120ebd0c9ee45a83e48ae431e65797c172403f
Build date: 2015-02-26 13:09:22-05
Xamarin addins: e88a2212bb7156a42ad6728b82e99846c3eea817

=== Operating System ===

Mac OS X 10.9.4
Darwin ShrutiMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 21 Chris Hamons 2015-04-08 18:08:30 UTC
I believe this is working in the cycle 5 builds. QA please retest with 2.0.
Comment 22 Rajneesh Kumar 2015-04-09 15:43:51 UTC
Hi @ Chris, today I have checked this issue with the latest builds of Cycle5, but I am still getting the same behaviour. 
Please refer the screencast: http://www.screencast.com/t/DC8kLFqT1

This issue still appears to me, hence I am reopening this issue. 
Also please let me know if am missing something to check this issue ?

Environment Info: https://gist.github.com/Rajneesh360Logica/f5eb06b54f7170b24c09
Application Output: https://gist.github.com/Rajneesh360Logica/6973143b2e19b7d921b7
Ide Logs: https://gist.github.com/Rajneesh360Logica/2bfdf1a88ae3833b52df
Build output: https://gist.github.com/Rajneesh360Logica/7239bd78e32ec0c4049c

Thanks...!
Comment 23 Chris Hamons 2015-04-15 15:28:30 UTC
Well it appears we'll have to do some more research, as the fix didn't take. Moving to next release.
Comment 24 David Lilley 2015-11-12 05:15:32 UTC
This works on El Captain and OS X Yosemite as I presume with the flat design the button no longer animates, but OS X 10.9 it fails presumably due to animation of it flashing
Comment 25 Chris Hamons 2016-06-10 17:31:44 UTC
So this appears to happen even when updating the sample to Unified Mobile 64-bit, but only on 10.9. From doing some research, it appears in 10.9 they spawn off a heartbeat thread to "throb" the button, and that is calling draw code \ frame. That's why KeyEquivalent = "\r" is important for this crash.

Now why calling frame is killing us, I am unsure.

The obvious work around is to don't set KeyEquivalent in 10.9 :(
Comment 26 Chris Hamons 2016-06-10 18:26:44 UTC
@David

So for some reason, invoking base.anything on this heartbeat thread kills us. I'm looking into why, so we can get a real fix, but the following is a less terrible workaround:

	[DllImport ("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSendSuper_stret")]
	public extern static void CGRect_objc_msgSendSuper_stret (out CGRect retval, IntPtr receiver, IntPtr selector);

        public override CGRect Frame
        {
            get
            {
		CGRect r = new CGRect ();
		CGRect_objc_msgSendSuper_stret (out r, this.SuperHandle, Selector.GetHandle ("frame"));
		return r; 
            }
            set
            {
                base.Frame = value;
            }
        }
Comment 28 David Lilley 2016-07-15 07:28:14 UTC
@Chris Thanks for the workaround, I am already working around by not using Frame on this.