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

Correct memory management in -awakeAfterUsingCoder: ?


  • Subject: Correct memory management in -awakeAfterUsingCoder: ?
  • From: Graham Cox <email@hidden>
  • Date: Tue, 5 May 2009 14:45:18 +1000

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.

Rough outline of the code:

// in object A:

- (id)	initWithCoder:(NSCoder*) coder
{
    ivarB = [[coder decodeObjectForKey:@"B"] retain];
}


// in object B:

- (id) awakeAfterusingCoder:(NSCoder*) coder
{
id replacement = [someStorageLibrary objectSimilarTo:self]; // <someStorageLibrary> is retaining <replacement>


if( replacement )
{
[self release]; // immediate release as per documentation
return replacement; //<--- to retain or to not retain, that is the question...
}
else
{
[someStorageLibrary addObject:self];
return self;
}
}




tia,

--Graham


_______________________________________________

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: Michael Ash <email@hidden>
    • Re: Correct memory management in -awakeAfterUsingCoder: ?
      • From: Greg Parker <email@hidden>
    • Re: Correct memory management in -awakeAfterUsingCoder: ?
      • From: Jonathan Hess <email@hidden>
  • Prev by Date: Re: Best way to save a CIImage to disk
  • Next by Date: Re: Programatically hiding file extensions in Finder
  • Previous by thread: Uncaching NSBundle's "objectForInfoDictionaryKey"
  • Next by thread: Re: Correct memory management in -awakeAfterUsingCoder: ?
  • Index(es):
    • Date
    • Thread