Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple change causing "Event registration is overwriting existing delegate" exception to fire when using C# events of NSWindowDelegate #6898

Open
whitneyschmidt opened this issue Sep 3, 2019 · 6 comments
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting iOS macOS Issues affecting macOS
Milestone

Comments

@whitneyschmidt
Copy link
Contributor

Original bug: https://forums.xamarin.com/discussion/comment/387768/#Comment_387768

public override void ViewWillAppear() { base.ViewWillAppear(); View.Window.WindowShouldClose += Window_WindowShouldClose; }

Exception:
Event registration is overwriting existing delegate. Either just use events or your own delegate: TestApp.WindowController AppKit.NSWindow+_NSWindowDelegate

It appears that Cocoa in some update is assigning a default delegate on Windows, and the exception is warning/complaining that we're overwriting it blindly. From what I can tell, this is a behavior change from Apple.

You can either:

Stop using the events and assign a View.Window.Delegate directly
OR
Set View.Window.WeakDelegate = null before assigning your first event
OR
If you want to completely disable the safety set NSApplication.CheckForEventAndDelegateMismatches to false

@whitneyschmidt whitneyschmidt added bug If an issue is a bug or a pull request a bug fix macOS Issues affecting macOS iOS Issues affecting iOS labels Sep 3, 2019
@whitneyschmidt whitneyschmidt added this to the Future milestone Sep 3, 2019
@chamons chamons changed the title Deprecate events to fix delegate assignment exception/warning caused by Apple change that creates default delegate Apple change causing "Event registration is overwriting existing delegate" exception to fire when using C# events of NSWindowDelegate Sep 3, 2019
@chamons
Copy link
Contributor

chamons commented Sep 3, 2019

We currently believe this is macOS only, but could affect iOS in a similar way.

We should research into what change from Apple is causing this, if we should special case it in our warning.

The general idea, warn when C# events cause the underlying delegate to be overwritten, is still solid however.

@ivanlabsii
Copy link

ivanlabsii commented Sep 3, 2019

Just to explain implications on my example. I had one subscription to WillClose in one one not so important window. As such it is almost never tested without particular reason and after it was compiled with the latest software it started to crash, users reported that.

This can happen to any app produced with Xamarin, actually it is certain that there will be more cases like this one. That's why I think that this needs to be fixed in some way (and I was a bit angry when the bug for this case I have submitted was closed: #6884 ), though I've stayed away from any angry comments there.

@BryanKipp
Copy link

I made some experiments on the issue and there are my 2 cents. We have two versions of the same app:

  • The first one was build a couple of months ago and it still works fine.
  • The second one was build from the same sources today and it crashes with "Event registration is overwriting existing delegate".

We run the apps on the same Mojave machine.

The code that was used to build the both apps is absolutely the same. The only difference is our build environment, I'm pretty sure we have installed Mono updates which go with Visual Studio for Mac updates.

Our current Mono version is 6.0.0.334.

Hope this helps.

@chamons
Copy link
Contributor

chamons commented Sep 20, 2019

Were they built with the same Xcode?

Often Apple will silently opt-in new behavior when you build with a new enough Xcode. My assertion, as noted above, is that Apple changed this behavior recently.

@BryanKipp
Copy link

BryanKipp commented Sep 20, 2019

Thank you for the quick reply.

Were they built with the same Xcode?

I'm not sure actually. I think we updated Xcode as well. I'll try to figure it out and reply back if I find some confirmation.

@BryanKipp
Copy link

Update: we did not update Xcode.

Just to double-check, I've made the following tests today. I've build the same app using two build configurations:

  1. Xcode 10.2.1 + Visual Studio for Mac 8.0.8; Mono 5.18.1.3; Xamarin.Mac 5.8.0.0
  2. Xcode 10.2.1 + Visual Studio for Mac 8.2.6; Mono 6.0.0.334; Xamarin.Mac 5.14.0.114 (it is the latest Stable update I got from the Visual Studio Updates at this moment).

The first app works fine, the second app crashes with "Event registration is overwriting existing delegate".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting iOS macOS Issues affecting macOS
Projects
None yet
Development

No branches or pull requests

4 participants