arachide
arachide

Reputation: 8066

NSMUrableArray alloc and init

I used codes below and set the breakpoint at a1, a2

NSMutableArray *aArray;

.....

@property (nonatomic,retain)    NSMutableArray *aArray;

......

NSMutableArray* a=[[NSMutableArray alloc]init] ;


for(int i=1;i<=31;i++)
    [a  addObject:[NSNumber numberWithInt:i]];
aArray=a; 


[a release];// a1

int i=0;// a2

the amount of objects in aArray is 31 but afeter the line [a release], the amount changes to 0

As I know 'release' only make the retain counter -1, but why it also removes all objects in the MutableArray?

Welcone any comment

Thanks

interdev

Upvotes: 0

Views: 91

Answers (2)

jfalexvijay
jfalexvijay

Reputation: 3711

use following statement;

aArray = [a retain];

If you use retain, aArray will not change.

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135558

With [a release], the reference count for the array becomes 0 and therefore, the array gets deallocated. When that happens, the array removes all objects from its contents in order to release them (because it has retained them before when they were added to the array).

Upvotes: 1

Related Questions