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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/adam%40caffeinatedcocoa.com

This email sent to [EMAIL PROTECTED]


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to