• 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: Matt Ball <email@hidden>
  • Date: Mon, 11 Jul 2005 00:07:49 -0400

No change. :/ Let me make sure I got the code right:

- (void)setImage:(NSImage *)anImage
{
	if(anImage != image && anImage != nil)
	{
		[image release];
        image = [anImage retain];
	}
}
- (NSImage *)image
{
	return [[image retain] autorelease];
}

With every other reference to "image" in this file changed to "[self image]."
Is that correct?

- Matt Ball

On 7/10/05, Charilaos Skiadas <email@hidden> wrote:
>
> On Jul 10, 2005, at 10:47 PM, Matt Ball wrote:
>
> Yeah, I did. [_NSZombie release] is either right before or right after
> [cell setImage] - I'm not sure (I'm pretty new to the debugger).
> [_NSZombie release] is #0, and [cell setImage] is #1. I suppose that
> means it is right before [cell setImage]?
>
> Yeah, that, exactly what it means. So your code goes into setImage, and then
> it calls release on image. But for some reason, image is already dealloced
> at that point.
> I would check to see if the "if" loop is called exactly when it should be
> called.
> Try to set things up as described in
> http://cocoa.mamasam.com/COCOADEV/2002/08/1/41613.php:
> I.e. describe both a getter and a setter (getter would be a method named
> image. You should never call your variable directly. i.e. throughout the
> rest of your code, instead of image you should have [self image]. The setter
> is your setImage method. ivar is image. See if this works.
> -getter {
>      return [[object retain] autorelease];
> }
> -(void)setter:newObject {
>      if (ivar==newObject) return;
>      [ivar release];
>      ivar=[newObject copy]; // or retain, depending on object & usage
> }
>
>
> - Matt Ball
>
> On 7/10/05, Charilaos Skiadas <email@hidden> wrote:
>
>
>
> On Jul 10, 2005, at 10:33 PM, Matt Ball wrote:
>
> I just turned on NSZombieEnabled and used it to track down where the
> offending NSImage was being allocated, and it is in
> shadowedImageWithImage, which means (I assume) that shadowCanvas is
> the problem (which makes sense, since that is what is being sent to
> the cell). Presumably the bad release doesn't happen inside
> shadowedImageWithImage, but later, inside the cell subclass.
>
> The error in the log was:
>
> *** Selector 'release' sent to dealloced instance 0x3d8e10 of class NSImage.
> Break at '-[_NSZombie release]' to debug.
>
> It you add this breakpoint and the trace your steps back from there?
>
>
> - Matt Ball
>
> On 7/10/05, Matt Ball <email@hidden> wrote:
>
> No, it doesn't still crash. However, then I have a bunch of memory leaks...
>
> - Matt Ball
>
> On 7/10/05, Charilaos Skiadas <email@hidden> wrote:
>
> Does it still crash if you replace:
>
>      return [shadowCanvas autorelease];
>
> With
>
>      return shadowCanvas;
>
> ??
>
> Also, just to make sure, your setImage does look like this?
>
> - (void)setImage:(NSImage *)anImage
> {
>      if(anImage != image && anImage != nil)
>      {
>           [image release];
>           image = [anImage retain];
>      }
> }
>
> I have to say, your code looks ok now, but maybe someone with more
> experience can spot what goes wrong. In general, NSZombieEnabled is
> what you want to look into to help you deal with autorelease issues.
> This is probably an autorelease issue. That's why your app crashes
> whenever the autorelease pool gets a chance to be released, which may
> vary.
> On Jul 10, 2005, at 9:55 PM, Matt Ball wrote:
>
>
>
> Actually, after further experimentation, it doesn't seem to be
> consistent on where the NSLogs stop. Sometimes it gets all the way to
> "End drawWithFrame" before crashing...
>
> - Matt Ball
>
> Haris
>
>
>
>
>
>
>  _______________________________________________
> 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
>
>
> Haris
>
>
>
>
>
>
> Haris
>
>
>
 _______________________________________________
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: Charilaos Skiadas <email@hidden>)
 >Re: Why does this leak memory? (From: Matt Ball <email@hidden>)
 >Re: Why does this leak memory? (From: Charilaos Skiadas <email@hidden>)
 >Re: 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: Charilaos Skiadas <email@hidden>)

  • Prev by Date: Simple C File IO Question (with code listing)
  • Next by Date: Need Help With Creating NSMutableNumber Class
  • Previous by thread: Re: Why does this leak memory?
  • Next by thread: Re: Why does this leak memory?
  • Index(es):
    • Date
    • Thread