locked
Why are we getting an NSView in our event handler? RRS feed

  • Question

  • User34526 posted

    We are handling key presses in a view, so we are using the following function:

    public override bool PerformKeyEquivalent (NSEvent theEvent) { //handle keypresses we handle and return true return false; //let unhandled keypresses pass on }

    We are getting errors because sometimes theEvent is NOT and NSEvent, but rather an NSView (actually an NSLabelCellView, which is one of our own subclasses of NSView). That is, when we put in code that spits theEvent.GetType().FullName to the console, we get "Musicnotes.Mac.NSLabelCellView". This is an INTERMITTENT bug. It doesn't happen on every keypress, but it does happen very frequently.

    We are getting similar errors in a different view where we are handling the MouseDragged event. Occasionally our app will just quit because NSApplication.SharedApplication.NextEvent(NSEventMask.LeftMouseDown | NSEventMask.LeftMouseDragged | NSEventMask.LeftMouseUp, NSDate.DistantFuture, NSRunLoopMode.EventTracking.ToString(), true); is returning an NSLabelCellView (InvalidCastException is what's being thrown).

    Is anyone else encountering this? Is there some bug in Xamarin causing this, or a setup issue on our end? Could this be caused by some weird threading issue? NSLabelCellView is not at all complicated, and definitely doesn't inherit from NSEvent. It's just a view that has an NSTextField inside it with some explicit properties set.

    --Christina

    Friday, July 17, 2015 8:41 PM

Answers

  • User13121 posted

    I downloaded the example you posted to bugzilla and was able to reproduce the problem, so I'll try to track it down

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 23, 2015 9:54 PM

All replies

  • User35201 posted

    That is very strange. The header even says:

    • (BOOL)performKeyEquivalent:(NSEvent *)theEvent;

    If you can come up with a small case that reproduces it, file a bug here.

    https://bugzilla.xamarin.com

    Friday, July 17, 2015 9:06 PM
  • User33830 posted

    For my few cents, I have seen this before,

    https://bugzilla.xamarin.com/show_bug.cgi?id=26281 and I will update it.

    I believe this is to do with Garbage Collection. Looking at the object that being passed I traced it back by the values it had to this bit of not the greatest code.

            public static nfloat HeightOfText(string sText,nfloat fWidth)
            {
            //TextView is subclassed NSTextView
                TextView  aTextView= new TextView();
                aTextView = sText;
                aTextView.Frame = new CGRect(0, 0, fWidth, 0);
                aTextView.AdjustLayout();
                nfloat fHeight = aTextView.Frame.Height;
                return fHeight;
            }
    

    Unsure why this was particularly had a bad effect on GC but it was for us. Since then I have removed this code and being good memory citizen of removing events so objects can be disposed properly and no longer have the issue.

    I hope this helps in some way...

    Dave

    Wednesday, July 22, 2015 10:09 AM
  • User34526 posted

    I'm still working on the constrained example, Chris, since I'm under a time constraint for a release, but I too believe that there is something weird in here happening with Garbage Collection.

    We are using the NSLabelCellView in a table. We think we start having the problem as soon as ReloadData is called on the tableview, causing all the cells to be reconstructed using the IntPtr constructor overload. My guess is that there is something deep down that is losing track of these IntPtrs and assigning them to the events that come in.

    Discovering this has had the side effect that we realized that we were reloading the table WAY more often than we needed to, and fixing that has reduced the frequency of the error significantly. However, we still will occasionally get it... so any time we have an event handler that relies on having an NSEvent, we are doing type checking on it before proceeding.

    This is a work around and we know it. If I get a constrained example working, I will post it to the bugzilla bug above.

    --Christina

    Wednesday, July 22, 2015 3:08 PM
  • User34526 posted

    I have attached a constrained example to the bugzilla report listed above. Hopefully, you guys will be able to track it down! --Christina

    Thursday, July 23, 2015 6:43 PM
  • User13121 posted

    I downloaded the example you posted to bugzilla and was able to reproduce the problem, so I'll try to track it down

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 23, 2015 9:54 PM