• 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: j o a r <email@hidden>
  • Date: Sat, 18 Jan 2003 17:50:23 +0100

*When* does it leak? You create a temporary, autoreleased, object like this:

[NSImage imageNamed:@"Connected"]

...then you make a copy of that object. Now you have two objects, one autoreleased and one with a retain count of one. After that you release the copied object, but the autoreleased object will remain for the duration of the current event loop. If you sample directly after releasing the copied object you would notice that you still have one object around - and this is as it should.

If you create a lot of autoreleased objects in a loop, you might consider using local autorelease pools. There is plenty of documentation on that topic, both in the docs from Apple, in the list archives, and online - notably on stepwise.com.

j o a r


On Saturday, Jan 18, 2003, at 13:59 Europe/Stockholm, Jamie Curmi wrote:

Maybe this is a stupid question. Could someone tell me why the following code leaks memory (under 10.2.3)?

NSImage * icon = [[NSImage imageNamed:@"Connected"] copy];
[icon release];

Of course, that isn't what my program does. But my program leaks, and I've simplified it down to that. I thought that the "release" would cleanup the copy of the icon I've made, but that doesn't seem to be the case.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Why does this leak memory?
      • From: Jamie Curmi <email@hidden>
References: 
 >Why does this leak memory? (From: Jamie Curmi <email@hidden>)

  • Prev by Date: Re: COM/DCOM and Objective-/Cocoa legacy
  • Next by Date: Everything You Always Wanted to Know About NSUserDefaults But Were Afraid to Ask
  • Previous by thread: Why does this leak memory?
  • Next by thread: Re: Why does this leak memory?
  • Index(es):
    • Date
    • Thread