• 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: Correct memory management in -awakeAfterUsingCoder: ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Correct memory management in -awakeAfterUsingCoder: ?


  • Subject: Re: Correct memory management in -awakeAfterUsingCoder: ?
  • From: Greg Parker <email@hidden>
  • Date: Mon, 4 May 2009 22:57:03 -0700

On May 4, 2009, at 9:45 PM, Graham Cox wrote:
I am trying to track down an over-release bug.

It looks like I'm doing something wrong in -awakeAfterUsingCoder: which leads to an object being insufficiently retained.

I have two objects, A and B. A loads B from an archive within - initWithCoder: while dearchiving and retains it. When B is dearchived, it can be replaced by another object by its - awakeAfterUsingCoder: method. If I detect that B should be replaced, I release the current B as per the documentation and return the replacement without doing anything regarding retain or release on it, as it's already retained by the code that supplied it.

Subsequently it is this object that is over-released.

If I retain the object before returning it in - awakeAfterUsingCoder:, the problem goes away, but this appears to violate normal memory management rules, so it's not clear that this is the correct solution. The documentation doesn't explicitly say anything about this either.

Two answers:

1. Retain it. Conceptually, you're balancing the [self release] that you did to the old-self. The old-self came in with one retain count that was not your responsibility to release. That means the new-self needs to go back out with one retain count that is not your responsibility. So you should retain it before returning. Whatever retain counts are held by the storage library are not sufficient by themselves to make the accounting book balance.

Strictly speaking, old-self's retain count could have been more than one. But if that happens you've probably already fallen afoul of answer number two:

2. Be very careful with replacing objects in -awakeAfterUsingCoder:. It doesn't work in the general case, so you had better be sure you're in a specific case that does work. See this thread for some discussion:
http://www.cocoabuilder.com/archive/message/cocoa/2001/10/10/48468
(That's an old thread, but as far as I know the newer NSKeyedArchiver didn't magically solve the problems.)



-- Greg Parker email@hidden Runtime Wrangler


_______________________________________________

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: Correct memory management in -awakeAfterUsingCoder: ?
      • From: Graham Cox <email@hidden>
References: 
 >Correct memory management in -awakeAfterUsingCoder: ? (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Integer as key in NSMutableDictionary
  • Next by Date: Re: Integer as key in NSMutableDictionary
  • Previous by thread: Re: Correct memory management in -awakeAfterUsingCoder: ?
  • Next by thread: Re: Correct memory management in -awakeAfterUsingCoder: ?
  • Index(es):
    • Date
    • Thread