• 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, 20 Nov 2008 16:04:06 -0500

Just to clarify:

They probably are identical, but not in the way everyone seems to be assuming.

To make sure I don't misrepresent anything, I'll just quote the rule:
"You take ownership of an object if you create it using a method whose name begins with "alloc" or "new" or contains "copy" (for example, alloc, newObject, or mutableCopy), or if you send it a retain message."


Nothing in that rule says or implies that objects you get through other methods are autoreleased.

I did a quick test and found that [[[NSString alloc] init] autorelease] and [NSString string] return the SAME instance. Thus, they are probably optimized to return a constant empty string, as there is no reason to have multiple instances of an empty string taking up memory in an app.

Now, I agree that it is mostly paranoia, but it is simply incorrect to say that any object you send autorelease to, or get through a method "whose name [DOES NOT] begins with "alloc" or "new" or contains "copy"" will be sent release in the next run loop.


I'll offer a more practical example. NSWorkspace is documented to be a singleton, and +sharedWorkspace returns "*the* NSWorkspace object associated with the process," i.e., there will always be a single instance on NSWorkspace available that will never be deallocated (except by the OS when the process terminates).


Thus, I sometimes see code that declares an instance variable "workspace," assigns it to [NSWorkspace sharedWorkspace] in an init method, then just uses it wherever, never bothering to retain it or anything.
While I think that's a pretty stupid way to save a little bit of typing, the documentation makes it clear that there is nothing wrong with doing that, because this instance will not disappear.


However, I think it *would* be a bad idea to call [[[NSWorkspace sharedWorkspace]retain]autorelease]
A good singleton will override -retain, -release, and -autorelease to do nothing and return an appropriate value. But since you know it's a singleton (through appropriate documentation), you really shouldn't be doing any memory management with it.



Adam Leonard


On Nov 20, 2008, at 5:12AM, Alexander Spohr wrote:

Just to clarify:

Am 19.11.2008 um 23:43 schrieb Joseph Crawford:

NSString blah = [[[NSString alloc] init] autorelease]

is identical to

NSString blah = [NSString string];


	atze

_______________________________________________

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


  • Follow-Ups:
    • Re: Autorelease Question
      • From: Klaus Backert <email@hidden>
    • Re: Autorelease Question
      • From: "Shawn Erickson" <email@hidden>
    • Re: Autorelease Question
      • From: Sherm Pendley <email@hidden>
References: 
 >Autorelease Question (From: "Carmen Cerino Jr." <email@hidden>)
 >Re: Autorelease Question (From: Joseph Crawford <email@hidden>)
 >Re: Autorelease Question (From: Alexander Spohr <email@hidden>)

  • Prev by Date: Is -rangeOfUserTextChange: supposed to wipe out marked text/diacritical marks?
  • Next by Date: Re: Is -rangeOfUserTextChange: supposed to wipe out marked text/diacritical marks?
  • Previous by thread: Re: Autorelease Question
  • Next by thread: Re: Autorelease Question
  • Index(es):
    • Date
    • Thread