• 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:01:50 -1000

The first two lines create a broken LayerCell instance that is leaked (where does copy, with its net retain count of 1, go? Nowhere!). You're returning something else entirely (the super's shallow copy). Only by coincide does this implementation not crash your application. LayerCell's only subclass-specific object ivar ends up with the correct net retain count, but for all the wrong reasons.

In other words, read the NSCopying implementation documentation that I pointed you to until you find the code sample that does basically exactly what you need to do. The only difference between the example in the Apple docs and the correct code for your subclass is the name of the ivar/accessor. I'm not sure how you could've read it several times and not caught on to any of this?

-bob

On Jul 11, 2005, at 2:48 PM, Matt Ball wrote:

Also, could someone tell me why this code is "bogus"?

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

- Matt Ball

On 7/11/05, Matt Ball <email@hidden> wrote:

Okay, maybe I'll be able to figure this out if someone can answer one question:

Why would copyWithZone: be called when I click on a row, but not when
I add a row?

- Matt Ball

On 7/11/05, Bob Ippolito <email@hidden> wrote:

Well, keep reading the memory management documentation, particularly
the implementation of NSCopying section when the superclass
implements copyWithZone:, until you understand it.

On Jul 11, 2005, at 2:09 PM, Matt Ball wrote:


I've reread the documentation several times, and every method I've
tried has resulted in a crash or weird editing behavior except for
this code...

- Matt Ball

On 7/11/05, Bob Ippolito <email@hidden> wrote:


Read the documentation.  That's totally bogus code!

On Jul 11, 2005, at 1:37 PM, Matt Ball wrote:



Okay, I think we're getting closer. Right now, cell editing is
working
as expected. However, now I am leaking an instance of my custom
cell.
Here's my code:

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

- Matt Ball

On 7/11/05, Bob Ippolito <email@hidden> wrote:




On Jul 11, 2005, at 1:08 PM, Bob Ippolito wrote:





On Jul 11, 2005, at 5:51 AM, glenn andreas wrote:






On Jul 11, 2005, at 10:40 AM, Matt Ball wrote:






Interface:

#import <Cocoa/Cocoa.h>

@interface LayerCell : NSTextFieldCell
{
    NSImage *image;
}

- (NSImage *)image;

@end

Implementation of NSCopying:

- (id)copyWithZone:(NSZone *)zone
{
     NSCell *copy = [[[self class] alloc] init];

     return copy;
}







This does not implement copy correctly - the results are a new
object that doesn't have an image, so it's not a copy of the
original.

You'll need to do something more like:

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





I'm pretty sure you want to be calling the super implementation of
copyWithZone:, but then you have another problem (probably,
anyway). See here:


http://www.mulle-kybernetik.com/weblog/archives/000472.html




And of course, reading the official documentation that tells you how to correctly implement NSCopying indispensable:

http://developer.apple.com/documentation/Cocoa/Conceptual/
MemoryMgmt/
Tasks/ImplementCopy.html

-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: 
 >Why does this leak memory? (From: Matt Ball <email@hidden>)
 >Re: Why does this leak memory? (From: Matt Ball <email@hidden>)
 >Re: Why does this leak memory? (From: glenn andreas <email@hidden>)
 >Re: Why does this leak memory? (From: Bob Ippolito <email@hidden>)
 >Re: Why does this leak memory? (From: Bob Ippolito <email@hidden>)
 >Re: Why does this leak memory? (From: Matt Ball <email@hidden>)
 >Re: Why does this leak memory? (From: Matt Ball <email@hidden>)

  • Prev by Date: Re: "untitled" vs "Untitled" (code correction)
  • Next by Date: Re: Why does this leak memory?
  • Previous by thread: Re: Why does this leak memory?
  • Next by thread: Re: Why does this leak memory?
  • Index(es):
    • Date
    • Thread