• 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: Charilaos Skiadas <email@hidden>
  • Date: Sun, 10 Jul 2005 19:25:11 -0500

On Jul 10, 2005, at 7:12 PM, Matt Ball wrote:

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

Here is your problem. This makes image point to anImage, but nothing is retaining anImage, so the moment you exit the method, image becomes invalidated, and boom.
Try:
- (void)setImage:(NSImage *)anImage
{
if(anImage != image && anImage != nil)
{
[image release];
image = [anImage retain];
}
}


Also, this line:
    image = [shadowCanvas retain];

will now leak the object that image was pointing to before this call. Also, there is a number of redundant lines, and calls to copy, retain and release, that I will leave to you to figure out. It will benefit you a lot more.
The key thing to understand, is that retain does not return a new pointer or something. It just increases the retain count of the object, and returns the object. I.e. whatever you set equal to [shadowCanvas retain] is pointing to exactly the same thing as shadowCanvas does, no difference at all between them. They are completely equivalent from that point on.


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

  • Prev by Date: Re: Documentation frustrations
  • 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