简体   繁体   中英

When do I release an NSMutableArray instance passed to multiple methods?

I have the following scenario:

class1
methodA
NSMutableArray *myArray=[[NSMutableArray alloc]init];
[class2 methodB:myArray];
[myArray release];
...

class2
methodB:(NSMutableArray) myArray{
[class3 methodB:myArray];
}
...

class3
methodC:(NSMutableArray) myArray{
  manipulate contents of myArray...
}
...

Is it appropriate to release myArray JUST in methodA or should I release it in each methodB and methodC? I want all three methods to have access to the same contents, ie, it is the same array being accessed in each method.

If you do not own myArray in methodB or methodC (that is you have not retained in that methods) then don't release in those methods. You own the array only in first method via alloc , so you release it only in methodA . So your approach is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM