• 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: Pb with object release
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pb with object release


  • Subject: Re: Pb with object release
  • From: Sherm Pendley <email@hidden>
  • Date: Wed, 9 Nov 2005 16:27:37 -0500

On Nov 9, 2005, at 12:59 PM, Simon Brée wrote:

NSImage *theDirIcon = [[NSWorkspace sharedWorkspace] iconForFile:theDirLocation];

Note - theDirIcon was not created with +alloc or -copy, and you haven't sent it a -retain. So, you don't need to release it.

Ok Sherm, I understand now.

If I want to control the release of my image, I should then change my code in :

NSImage *theDirIcon = [[NSImage alloc] init];
[[NSWorkspace sharedWorkspace] iconForFile:theDirLocation];

No, you shouldn't. If you want to keep a reference to an object for later use, and you didn't create it with +alloc or -copy, just send the object a -retain message. Then it's your responsibility to match that -retain with a -release (or an -autorelease) later, when you're done with it.


BTW, is there a way to check the reference couter of a particular object in the debugger for exemple ?

Yes, but I *highly* doubt that you need to do that. What you need to worry about is balancing your own +alloc/-copy/-retain vs -release/- autorelease calls. The reference count is an internal detail that can be (and often is) affected by code other than yours.


Cocoa's memory management is really quite straightforward, just a handful of guidelines to follow. Try not to over-think or over-manage it. Lots of folks do that, and wind up tying themselves into knots. A good introduction is here:

	<http://www.stepwise.com/Articles/Technical/HoldMe.html>

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
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: 
 >Fwd: Pb with object release (From: Simon Brée <email@hidden>)

  • Prev by Date: Re: NSTextField text input
  • Next by Date: Getting NSXMLDTD to initialize
  • Previous by thread: Re: Pb with object release
  • Next by thread: Custom view drag to NSOutlineView and Finder
  • Index(es):
    • Date
    • Thread