• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Delegate methods[SOLVED]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Delegate methods[SOLVED]


  • Subject: Re: Delegate methods[SOLVED]
  • From: Neil Earnshaw <email@hidden>
  • Date: Wed, 20 Aug 2003 22:46:49 +0100

I knew as soon as I opened my mouth that someone would come up with all sorts of references to prove me wrong. :)

Yeah, you're right. It's not the notification center that's retaining the observed object, it's the NSNotification it creates that does it.

If you want to see the problem in action I've put a little PB project in Public space on .mac. You should be able to get it using iDisk Utility with member name 'ndearnshaw'. Any problems and I can e-mail it to you. Its only about 12K.

Build and run NoteTest.

A Document window opens. Click the red close button on the window and you'll see NSLog messages coming from:
i) the document when it is deallocated;
ii) the window controller when it receives windowWillClose: and;
iii) the window controller when it is deallocated.

Next, use File->New to open a new document and press the Remove Observer button. Close the window using the red window button and you'll see that the document and window controller are not deallocated and windowWillClose: is not received.

Use File->Quit to exit the application. Now the document and window controller are not deallocated. Run it in ObjectAlloc and it seems that when you quit you leaked all the window controllers, documents and windows for which you pressed the 'Remove Observer' button. A couple of days developing with this gotcha in place and your machine will need a reboot.

The documentation at file:///Developer/Documentation/Cocoa/TasksAndConcepts/ ProgrammingTopics/Notifications/index.html says:

If an observer later decides it no longer needs to receive notifications (for example, if it is being deallocated), it can remove itself from the notification center's list of observers with the methods removeObserver: or removeObserver:name:object: (removeObserver in Java).

Notice it says the observer 'can' remove itself. It doesn't say that using removeObserver: may trash the delegate messages you are expecting and result in your memory leaking away. Us newbies need this kind of information!

Hope this helps my fellow newbies :-) Think I'll reboot now...

-Neil


On Wednesday, August 20, 2003, at 06:40 PM, Dave Camp wrote:

On Wednesday, August 20, 2003, at 06:20 AM, Neil Earnshaw wrote:

Others seem to be managed by the window adding the delegate as an observer. The delegate doesn't know that it has been made an observer and the documentation doesn't explicitly say,

It's documented in the Windows and Panels docs:

"NSWindow offers observers a rich set of notifications, which it broadcasts on such occurrences as gaining or losing key or main window status, miniaturizing, moving or resizing, becoming exposed, and closing. Each notification is matched to a delegate method, so an NSWindows delegate is automatically registered for all notifications that it has methods for."

Another note for newbies: The documentation for notifications clearly states that the observer is not retained. Unfortunately, it doesn't point out that the observed object _is_. So, if you have any cyclic notification chains it is _not_ sufficient to removeObserver:self in your dealloc method because dealloc will never get called. Instead, one of the objects in the circle must removeObserver:self outside the dealloc method. Once the chain has been broken, then the whole lot will get cleaned up (as long as dealloc removes the object from the notification center).

Maybe I'm looking at the wrong thing, but the documentation for addObserver:selector:name:object: says otherwise.

"The notification center does not retain anObserver or anObject. Therefore, you should always send removeObserver: or removeObserver:name:object: to the notification center before releasing these objects."


Dave
-
It's not denial. I'm just very particular about the reality I choose to accept. -Calvin
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


Neil Earnshaw
Consultant Software Engineer
Object Software Engineers Ltd
email@hidden
Tel : 01747 854 852
Mbl : 07870 209 102
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Delegate methods[SOLVED] (From: Dave Camp <email@hidden>)

  • Prev by Date: Re: authorisation
  • Next by Date: Simple question: Why does this work?
  • Previous by thread: Re: Delegate methods[SOLVED]
  • Next by thread: Safari Form Values access?
  • Index(es):
    • Date
    • Thread