Re: Literal NSStrings
Re: Literal NSStrings
- Subject: Re: Literal NSStrings
- From: Jens Alfke <email@hidden>
- Date: Mon, 04 Jul 2011 18:05:01 -0700
On Jul 4, 2011, at 5:23 PM, William Squires wrote:
> is there ever a situation in which (properly written) client code could call this and trip over the memory management rules?
No, it’s fine. The rule is that if you got it from +alloc or -copy you have to release or autorelease it; but if you didn’t, you don’t care. So returning a string literal is OK.
> I would guess that literal NSStrings are (effectively) retained, since they're not going anywhere (they're literal constants, after all), but they're not obtained by "New", "alloc", or "copy", which - according to the memory management rules, means you should retain them because they were autoreleased. Which is true?
Proper procedure is to retain/copy them if you’re going to assign them into an instance or static variable, but in this case it doesn’t really matter because they’re never going to be dealloced.
On the other hand, I think it’s better to be safe, as things like this can change. For example, for a long time NSFont objects weren’t ever dealloced, so some developers got lazy about retaining them and just assigned them directly to globals. Then in 10.5(?) they did start getting dealloced when not used, which caused some apps to crash.
—Jens_______________________________________________
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