[10.11.1] Removing NSTextView from layout manager disables `delegate.textDidChange()` calls. [Sample included]

Originator:bjhomer
Number:rdar://23665687 Date Originated:25-Nov-2015
Status:Open Resolved:
Product:OS X SDK Product Version:10.11.1
Classification:Serious Bug Reproducible:Always
 
Summary:
Situation: you have multiple NSTextViews linked to a single layout manager. You have a delegate set on the first text view, and are watching for "textDidChange()" calls. You do something that causes one of the other text views (NOT the first one) to be removed from the layout manager and deallocated.

Result: Now the delegate of the first text view is still set, but it no longer receives textDidChange() calls. This happens because in NSTextView's -dealloc method, it calls -[NSNotificationCenter removeObserver:name:object:], but critically, it does so passing the first text view as the "object", even if the deallocated text view is not the first text view.

Steps to Reproduce:
1. Build and run the attached sample project.
2. Type in the text view to verify that textDidChange() is being called.
3. Click "Trigger Bug" to remove the 2nd text view.
3. Type again in the text view.

Expected Results:
textDidChange() should still be called, as the view controller is still the delegate of the first text view.

Actual Results:
Other delegate methods are called, but textDidChange() is no longer called.

Version:
OS X 10.11.1
Xcode 7.1.1

Notes:
I've verified that NSTextViewDelegate.textDidChange() is routed by adding the delegate as an observer of NSTextDidChangeNotification when the delegate is set. Thus, if that notification observation is removed, the delegate method will no longer be called. Set a breakpoint on -[NSNotificationCenter removeObserver:name:object:] right before clicking "Trigger Bug" in the sample app to see exactly where the observer is being removed. Note that the "object" in the call is not the text view being deallocated.

Configuration:
Happens on both 10.10.4 and 10.11.1


Attachments:
You can find a sample project at https://github.com/bjhomer/TextViewDelegateBug_23665687.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!