-
Notifications
You must be signed in to change notification settings - Fork 283
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
NSNotificationCenter should automatically unregister released observers #481
Comments
The obvious way to implement this sort of thing would be rewriting to use weak references to observers, but that's a runtime dependent feature. |
Thanks for your feedback @rfm. I’m not familiar with what weak reference support looks like with GCC, but since this is a new-ish feature on Apple platforms as well, wouldn’t it be ok to support it with libobjc2 only if it’s only feasible there? I see this as an improved implementation, similar to the new KVO implementation that also requires runtime features. Normal add/remove observer would of course still work with all runtimes. For us the main reason we’d like to see this in GNUstep is that our devs mainly write code on Apple platforms, and if they forget to remove an observer somewhere we won’t notice on those platforms, plus it’s often difficult to catch these mistakes as the app might not crash immediately if the observer is released. Having this work the same across (our) platforms would eliminate this potential source of errors. |
Generally it's a big problem for maintainability and support to have different behaviors on different systems and is antithetical to one of the guiding principles of the project: portability ... we should be able to write GNUstep code and it should run on all platforms where GNUstep core libraries run. We already have far too much stuff that is not completely portable (mostly due to external dependencies), and the worse it gets the greater the pushback against anything more, so the fact that we are already trying to integrate a new KVO implementation is a strong reason not to add more runtime specific code. Arguably we should iron out quite a lot of the outstanding differences before doing anything that adds new ones. That being said, I think this could be done in two stages:
|
I'd be very happy to see a contribution of (1) above, since that would also allow us to fix weak pointer based incompatibilities elsewhere in base, I think a C language implementation based on the algorithms in arc.mm in libobjc2 would be great for portability. |
Actually, I think I'll look into adding weak reference support (emulation of the runtime features) for use with gcc and the gnu runtime anyway, because having working weak pointer versions of NSPointerArray, NSHashTable and NSMapTable would be good. |
Great, thank you. |
I have preliminary (working) code for weak references with gcc in a new 'weakref' branch. As far as I know the only problem with it is that it's currently going to be too slow because every object deallocation involves locking and a hash table lookup. In libobjc2 this is mostly avoided by marking each weakly referenced object, and using a lock-free check at deallocation to see whether the object being deallocated has weak references. Probably we need something similar. |
This should now be working on the 'weakref' branch for both the classic gnu runtime and for the modern ruuntime. |
Thank you Richard, that’s great! |
On newer Apple OS releases, NSNotificationCenter no longer requires users to unregister observers that get released. It would be helpful if GNUstep could do the same.
https://developer.apple.com/documentation/foundation/nsnotificationcenter/1415360-addobserver?language=objc
The text was updated successfully, but these errors were encountered: