• 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: Autorelease question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Autorelease question


  • Subject: Re: Autorelease question
  • From: Adam Leonard <email@hidden>
  • Date: Thu, 3 Apr 2008 19:40:27 -0700

You quoted the key memory management rule yourself from Hillegass's book :)

You are making this too complicated. Just read this: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html#/ /apple_ref/doc/uid/20000994

There are virtually no exceptions to those rules in the Cocoa frameworks (if there are, they will be documented!)


looking at the retainCount property won't tell you about was
object autoreleased or not
Correct, you shouldn't care


documentation doesn't tell
because everything follows the rules

I posted my code you can see all 6 creation calls
And none of them have "alloc","new", or "copy" anywhere in the method name, so NONE of them should be released by you.

The only think you need to release is the pool, because it was created with -alloc


how can you explain that
NSString *nsFontName = [NSString stringWithCString:fontName encoding:NSMacOSRomanStringEncoding];
can be released, but
NSString *textString = [NSString stringWithCString:"Hello From Cocoa" encoding:NSMacOSRomanStringEncoding];
can not
so what's the difference?

Just because the first doesn't crash when it is released does not mean that it *should* be released. (That's why I said you cannot know that an object was actually autoreleased- but you should not care!). Just assume both will be gone when you release the pool. If you want them longer, retain them.


Whether an object is autoreleased by someone else is an implementation detail. It can change in a future release. The fact is, neither of those methods have "alloc" in the name, so neither need to be released by you.

Don't think too much into all this. As long as you fulfill your side of the contract, trust that Cocoa will fulfill its side.


Adam Leonard

thanks


On Apr 3, 2008, at 10:10 PM, Rob Keniger wrote:


On 04/04/2008, at 11:54 AM, Dmitry Markman wrote:
if I comment all those releases (excerpt [pool release])
then application won't crash

That's because all the methods you use to create the objects return autoreleased objects which don't need to be released manually.


that probably means that some of the objects
(stringAttributes, font, cocoaColor, context, textString or nsFontName)
were already in the pool and I shouldn't release them
also if I call retain for every above object and call release the everything will be fine with [pool release]
but why should I retain them if I have no intention to share them?


Listen to what Chris says:

You're releasing things you shouldn't be. Take some time to read the memory management documentation.


Read Apple's memory management documentation as well as the many, many articles about Objective-C memory management on the web. Read "Programming in Objective-C" by Stephen Kochan.

You are doing it wrong, that's why it's crashing.

--
Rob Keniger



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

Dmitry Markman

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Autorelease question (From: Dmitry Markman <email@hidden>)
 >Re: Autorelease question (From: Robert Claeson <email@hidden>)
 >Re: Autorelease question (From: Jens Alfke <email@hidden>)
 >Re: Autorelease question (From: Dmitry Markman <email@hidden>)
 >Re: Autorelease question (From: Dmitry Markman <email@hidden>)
 >Re: Autorelease question (From: Rob Keniger <email@hidden>)
 >Re: Autorelease question (From: Dmitry Markman <email@hidden>)

  • Prev by Date: Re: Autorelease question
  • Next by Date: Re: NSCondition easter-egg hunt
  • Previous by thread: Re: Autorelease question
  • Next by thread: Re: Autorelease question
  • Index(es):
    • Date
    • Thread