• 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: Why does this leak memory?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why does this leak memory?


  • Subject: Re: Why does this leak memory?
  • From: Bob Ippolito <email@hidden>
  • Date: Mon, 11 Jul 2005 15:30:45 -1000


On Jul 11, 2005, at 3:22 PM, Jeff Laing wrote:

Check out this link that was the first hit on google searching for
"copyWithZone:zone":

http://www.cocoabuilder.com/archive/message/cocoa/2004/11/5/120922


But don't take what it says literally - make sure you read the entire thread
which points out some fatal flaws again to do with reference counting...

... which you repeat below!

*That* is the main reason I hate the way people respond here with 'look
harder next time - I found it in one google hit'. Because so many times
I've found misinformed opinions just as prevalent as informed ones.


Would it really have killed everyone to point out exactly what was wrong
with this code, rather than just say "its bogus, go read the docs properly".



- (id)copyWithZone:(NSZone *)zone
{
    LayerCell *copy = [[[self class] alloc] init];
    [copy setImage:[self image]];
    return [super copyWithZone:zone];
}


For the record, it would look better as

- (id)copyWithZone:(NSZone *)zone
{
LayerCell *copy = [super copyWithZone:zone]; // get superclass to
make a copy
[copy setImage:[self image]]; // then copy
our members across
return copy; //
before returning the copy
}


At least, thats my novice opinion.

Which is still wrong! As per the Apple documentation, the super's copy is going to have a shallow copy of all ivars, including the image, so it will have a net retain count of -1 when the setter releases it. If you had read the documentation, you'd know that the correct code would first overwrite copy->image with nil before invoking the setter.


I didn't blindly paste the correct code because it's important to understand what you're doing. I went as far as specifying the section of the page with official definitive information that would give the insight required to correctly implement this.

-bob

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >RE: Why does this leak memory? (From: Jeff Laing <email@hidden>)

  • Prev by Date: Re: Why does this leak memory?
  • Next by Date: Re: Custom NSSlider Action Not Firing
  • Previous by thread: RE: Why does this leak memory?
  • Next by thread: Re: Why does this leak memory?
  • Index(es):
    • Date
    • Thread