• 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: Get error message about registered observers when Object receives dealloc message
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get error message about registered observers when Object receives dealloc message


  • Subject: Re: Get error message about registered observers when Object receives dealloc message
  • From: Quincey Morris <email@hidden>
  • Date: Sat, 29 Aug 2009 10:28:53 -0700

On Aug 29, 2009, at 06:00, Roland King wrote:

Well first off you're not really, really doing anything wrong, that message is in the wrong place (in my opinion), it should only come up when the NSObject dealloc is called if things haven't been unobserved. There's a comment in this thread about this having been fixed, hopefully it is.

If I understand what's been said in this thread, Andreas *is* doing something wrong. He's in good company, though, because the retain/ release mechanism has traditionally allowed many Cocoa developers to do it wrong over the years. It's only now that the chickens are coming home to roost.


There are actually two things wrong here. One is that it's not *generally* safe to manage lifetimes of mutually dependent objects in their 'dealloc' methods. In this case, Andreas has said, observing objects are being released in the observed object's 'dealloc', and the released (and therefore deallocated) observing objects are messaging the partially torn down observed object from their own 'dealloc'. With very careful coding, Andreas *might* be able to make this kind of thing safe *for the part of the object behavior that he controls*, but he can't make it safe for the parts he doesn't control (e.g. behavior inherited from the frameworks).

The second is that one of the behaviors he doesn't control -- unregistration of observers -- is not permitted during the 'dealloc' of the observed object, and must be done before that. That's what the log message is trying to say. The bug fixes described in the Snow Leopard release notes *don't* permit this requirement to be ignored. [In fact, the notes explicitly say that the consequences are memory leaks and observation info getting attached to the wrong object.] Instead, Snow Leopard corrects the detection of the problem in the retain/release case. That's why log messages have suddenly started appearing. Andreas' implementation was always wrong, but it's only being reported correctly now.

This is not news to anyone who's been using garbage collection, which has always had the same restriction, and enforced it. The solution (as stated earlier in this thread) is to unregister the observers of an observed object while it is still owned (RR: retained; GC: referenced) by something.

This sort of "release your resources" cleanup is sometimes much harder to do without relying on 'dealloc', because you lose the automatic "notification" of the end of an object's lifetime that 'dealloc' implies. Nevertheless, it's necessary because of KVO registrations, and it's necessary because of garbage collection.

I wish the frameworks did provide some kind of end-of-lifetime detection that would trigger a notification before object destruction begins. This would be something like a counterpart to 'awakeFromNib', which is a kind of beginning-of-lifetime notification after object initialization is complete. But there isn't anything like that in the frameworks (yet), so we have to do it the hard way.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Get error message about registered observers when Object receives dealloc message
      • From: "Sean McBride" <email@hidden>
    • Re: Get error message about registered observers when Object receives dealloc message
      • From: Roland King <email@hidden>
    • Re: Get error message about registered observers when Object receives dealloc message
      • From: Roland King <email@hidden>
    • Re: Get error message about registered observers when Object receives dealloc message
      • From: Andreas Grosam <email@hidden>
References: 
 >Get error message about registered observers when Object receives dealloc message (From: Andreas Grosam <email@hidden>)
 >Re: Get error message about registered observers when Object receives dealloc message (From: Roland King <email@hidden>)
 >Re: Get error message about registered observers when Object receives dealloc message (From: Andreas Grosam <email@hidden>)
 >Re: Get error message about registered observers when Object receives dealloc message (From: Roland King <email@hidden>)

  • Prev by Date: Re: Normalize an NSAttributedString
  • Next by Date: Re: "Format not a string literal and no format arguments"
  • Previous by thread: Re: Get error message about registered observers when Object receives dealloc message
  • Next by thread: Re: Get error message about registered observers when Object receives dealloc message
  • Index(es):
    • Date
    • Thread