简体   繁体   中英

NSMutableArray release causes crash

I am using XCode for developing an iPhone app. I am new to this platform and need some help with a particular issue...

I have a method that processes some data and returns two integer values as NSNumber wrapped into a NSMutableArray.

Here is the method:

-(NSMutableArray *)processPoints:(int) x:(int) y
 {
 NSMutableArray *mutArray = [[NSMutableArray alloc] initWithCapacity:3];
 int x9,y9;

 // ...do some processing...

 NSNumber* xNum = [NSNumber numberWithInt:x9];
 NSNumber* yNum = [NSNumber numberWithInt:y9];

 [mutArray addObject:xNum];
 [mutArray addObject:yNum];

  return [mutArray autorelease];
 }

I call the above method from another method, where I copy the NSNumber stuff into local variables and then release the local copy of NSMutable array.

But the app crashes when releasing this NSMutable array (variable 'mutArray').

Here is the method:

-(void)doNinjaAction
 {
   NSMutableArray* mutArray = [self processPoints: x :y];

   NSNumber* s1 = [[mutArray objectAtIndex:0] retain];
   NSNumber* s2 = [[mutArray objectAtIndex:1] retain];

   x = [s1 integerValue];
   y = [s2 integerValue];

   //...proceed with other stuff...

   [mutArray autorelease]; //this is where the system crashes. same for 'release'            
                           //instead of 'autorelease'
  }

Can you please explain where I am going wrong with the process of memory release.

My understanding of the process is a bit shaky. Please help.

Because you're overreleasing the array. You alloc-init it in processPoints: , then you autorelease it - that's correct, this is how you dispose of its ownership.

After that, you don't need to and must not autorelease or release it once again. This is not malloc() from the standard library.

when you call the statement

   NSMutableArray* mutArray = [self processPoints: x :y];

This itself acts as autorelease.

Hence releasing the array explicitly will cause the app to crash.

You are releasing mutArray more then once. Once in processPoints function and again in doNinjaAction .

To resolve the crash remove :

[mutArray autorelease];
-(NSMutableArray *)processPoints:(int) x:(int) y
 {
 NSMutableArray *mutArray = [[NSMutableArray alloc] initWithCapacity:3];
 int x9,y9;

 // ...do some processing...

 NSNumber* xNum = [NSNumber numberWithInt:x9];
 NSNumber* yNum = [NSNumber numberWithInt:y9];

 [mutArray addObject:xNum];
 [mutArray addObject:yNum];

[mutArray autorelase];
  return mutArray;
 }

try this one it'll resolve it.

-(NSMutableArray *)processPoints:(int) x:(int) y
 {
 NSMutableArray *mutArray =[[[NSMutableArray alloc] initWithCapacity:3]autorelease];
 int x9,y9;

 // ...do some processing...

 NSNumber* xNum = [NSNumber numberWithInt:x9];
 NSNumber* yNum = [NSNumber numberWithInt:y9];

 [mutArray addObject:xNum];
 [mutArray addObject:yNum];

  return mutArray;
 }

As @H2CO3 and @AppleDelegate suggested, it is right.

Still I would suggest to use ARC and convert your project to ARC enabled.

Go to Edit->Refactor->Convert to Objectiv-C ARC

Then you dont need to do any releases anywhere. It will take care itself of all the releases :)

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