Re: Memory Management
Re: Memory Management
- Subject: Re: Memory Management
- From: "John C. Randolph" <email@hidden>
- Date: Sun, 5 Aug 2001 21:33:40 -0700
On Sunday, August 5, 2001, at 05:51 PM, Nat! wrote:
[snippage]
There's another thing you should watch out for, which is that -init
might get called more than once. I would alter the above to:
Calling the initializer twice is a bug.
Not necessarily. I write my initializers so that I can re-init an
object rather than -dealloc it and -alloc a new one. This can be a real
time-saver when I need a lot of objects for a short time each. Also, I
have run into cases where AppKit code initialized my objects more than
once. That may be a bug, but it happens.
It's specified that +initialize
may be called more than once but not -init.
- (id)init
{
if (self = [super init]) //initialize the object using the super
class's method
{ // super init worked.
if (myArray || myArray = [[NSArray alloc]
initWithObjects:@"Hello", @"World", nil]) //init your array *once*
return self;
AFAIK when alloc fails (out of memory) a NSMallocException will be
raised. What is the second check for ?
The second check is for -initWithObjects: returning nil.
BTW, you missed the bug in that line. It should be:
if (myArray || (myArray = [[NSArray alloc] initWithObjects:@"Hello",
@"World", nil])) //init your array *once*
-jcr
For every complex problem there is an answer that is clear, simple, and
wrong. -- H L Mencken