Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Foo new] vs [[Foo alloc] init]:



On Feb 18, 2008, at 6:53 AM, Gregory Weston wrote:

On Feb 17, 2008, at 6:05 PM, Bill Bumgarner wrote:

If you are overriding +alloc for whatever reason, you better had override +allocWithZone:, too.

The real point is that there's no formal requirement that you do so. It only becomes an issue if you make unwarranted assumptions about the implementation.

If you're familiar with Cocoa, you know that the documentation is somewhat lacking. That sucks, but on this particular point the documentation seems reasonably clear. The Discussion for alloc begins as follows:


"The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0. The new instance is allocated from the default zone—use allocWithZone: to specify a particular zone."

Ok, it even mentions allocWithZone: right at the beginning, so let's read its Discussion:

"The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for its other instance variables is set to 0. If zone is nil, the new instance will be allocated from the default zone (as returned by NSDefaultMallocZone)."

Now, it's not stated exactly and formally, but if you're familiar with Cocoa, you know that it's an extremely common pattern to have methods doSomething and doSomethingWithArgument:, where doSomething is implemented as doSomethingWithArgument:nil. Thus, if you're familiar with Cocoa, your first thought after reading those Discussions above is that maybe alloc is implemented as allocWithZone:nil.

And if you're not familiar with Cocoa, it's not the greatest idea to override such a crucial method as alloc.

-Jeff

_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Bill Bumgarner <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Bill Bumgarner <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Bill Bumgarner <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Bill Bumgarner <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Bill Bumgarner <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Bill Bumgarner <email@hidden>)
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.