Re: Disposing NSArray
Re: Disposing NSArray
- Subject: Re: Disposing NSArray
- From: Vince DeMarco <email@hidden>
- Date: Thu, 31 May 2001 22:04:13 -0700
On Thursday, May 31, 2001, at 04:03 PM, Youngjin Kim wrote:
What's the differerence between two?
Both array1 and array2 are locally instantiated in a method.
-(void) doSomething {
NSArray* array1;
NSArray* array2;
array1 = [[NSArray arrayWithObjects:s1,s2,s3,s4,nil] autorelease];
array2 = [[[NSArray alloc] initWithObjects:s1,s2,s3,s4,nil]
autorelease];
...
}
I'm getting signal raised with array1. but array2 work fine.
array1 is already autoreleased.
The rules for release, retain , autorelease are simple.
If you alloc something it just like you sent retain to the object, so
you release or autorelease it.
If do a copy or a mutableCopy its also the same as sending a retain, so
you also release or autorelease it.
If you retain something you must release or autorelease it.
If you never sent a retain (or equivalent) method to an object then by
default it is autoreleased and will go
away on its own.
Also, in my code i always try to release an object instead of
autoreleasing it, if that is possible (ie without completely re-writting
it)
vince