开发者

NSArray Release crash

开发者 https://www.devze.com 2023-02-04 08:06 出处:网络
In my code, i\'m creating 5 sets of objects, and 5 NSArrays containing those objects. At the end of my method, two of the arrays release properly, but the other three crash my application.

In my code, i'm creating 5 sets of objects, and 5 NSArrays containing those objects. At the end of my method, two of the arrays release properly, but the other three crash my application.

Creating

UIImageView *image0 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TankAxe.png"]];  
NSArray *imageArray = [[NSArray alloc] initWithObjects:image0, nil];

NSString *name0 = [NSString stringWithString:@"Pistol"];
NSArray *nameArray = [[NSArray alloc] initWithObjects:name0, nil];

NSNumber *price0 = [NSNumber numberWithInt:100];
NSArray *priceArray = [[NSArray alloc] initWithObjects:price0, nil];

NSNumber *round0 = [NSNumber numberWithInt:0];
NSArray *roundArray = [[NSArray alloc] initWithObjects:round0, nil];

NSNumber *priceRound0 = [NSNumber numberWithInt:0]; 
NSArray *priceRoundArray = [[NSArray alloc] initWithObjects:priceRound0, nil];

Releasing

[name0 release];
[nameArray release]; //Releases properly

[image0 release];
[imageArray release]; //Releases properly

[price0 release];
NSLog(@"%i",[priceArray retainCount]); //Returns 1
[priceArray rel开发者_如何学Pythonease]; //Source of the crash

[round0 release];
[roundArray release]; //Also crashes

[priceRound0 release];
[priceRoundArray release]; //Also Crashes

Anybody know how to properly release the arrays containing NSNumbers?


price0, name0,round0, and priceRound0 should not be released. They were not created with alloc, and will be autoreleased by the methods that returned them.

Once you release an object that you shouldn't, the heap is corrupted, and the program could crash at any time.

The easiest way to debug this is to turn on zombies (Tip #1):

http://www.loufranco.com/blog/files/debugging-memory-iphone.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号