Re: Memory allocation issues with NSObject and NSString
Re: Memory allocation issues with NSObject and NSString
- Subject: Re: Memory allocation issues with NSObject and NSString
- From: Ben Trumbull <email@hidden>
- Date: Mon, 19 Jan 2009 15:14:15 -0800
On Sun, Jan 18, 2009 at 11:21 PM, Bill Bumgarner <email@hidden> wrote:
2. If I have an NSObject class and I define an init messge, do I
have
to do self = [super init] or is that not important when extending
from
NSObject.
Yes, it is important.
But it does next to nothing.
For now. Someday, that might change.
I sure hope not! The documentation for the method states:
"The init method defined in the NSObject class does no initialization;
it simply returns self."
Making it do anything else would be a serious breach of the API
contract.
That is a matter of perspective. And why lawyers plague the world.
"no initialization" is not the same as "absolutely not a single CPU
instruction". For example, this tech writer might have meant "no
initialization of this object", which would be obvious, not realizing
that someone else would read that and wrongly infer that they could
safely skip using it correctly as demonstrated in the code box right
next to the words.
The NSObject alloc/init pair is responsible for implementing NSZombie
support, and ObjectAlloc integration. They also call into the ObjC
runtime, so fundamental changes to class or object initialization are
possible beyond the literal implementation of NSObject. An example of
that would be the compile flag 'Call C++ Default Ctors/Dtors in
Objective-C' for properly initializing ivars of an Objective-C object
that are themselves C++ objects.
Now that happens courtesy of +alloc, so you may get away with abusing -
init. Still, if you read the documentation and conclude based on the
English verbiage that you are free to ignore the documented code
patterns right there next to it in the -init method documentation
(which call super and assign to self), you're liable to hurt yourself.
There are times and places to not use +alloc, or even cheat with -
init. But then, you get what you get and roll an SU when you're
wrong. caveat emperor.
Future proof software should follow the documented coding patterns.
Several AppKit and Core Data classes unmercifully require it.
- Ben
_______________________________________________
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