"-[CFNumber intValue]: message sent to deallocated instance" and no idea
i have a problem with a exc bad access. I already turned in NSZombieEnabled, but cannot figure out why this problem will be caused. How the cartInstance Array is defined you can see below in the following function. It's a NSMutableArray with several NSMutableDictionaries The error occurs every time my counter i reaches 13. Then i get a exc bad access and the message as shown in the title. Here is some code:
-(void)addToCart:(NSDictionary *) article{
if(article!=nil){
NSString * amount = @"amount";
NSString * articleId = @"articleId";
NSString * detailKey = @"detailKey";
NSString *curId = [article objectForKey:@"articleId"];
//check if article already in shopping cart
if([cartInstance count]>0)
{
for(int i=0;i<[cartInstance count];i++) {
NSString *tempStr = [[cartInstance objectAtIndex:i] objectForK开发者_如何学Cey:articleId];
if([tempStr isEqual:curId]) {
NSNumber *newAmount = [[cartInstance objectAtIndex:i] objectForKey:amount];
NSLog(@"AddtoCart");
int tempInt = [newAmount intValue]+1;//Here is where the magic happens
newAmount = [NSNumber numberWithInt:tempInt];
[[cartInstance objectAtIndex:i] setObject:newAmount forKey:amount];
[newAmount release];
return;
}
}
}
NSDictionary *details = article;
NSDictionary *shoppingItem = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1],amount,
curId,articleId,
details,detailKey,
nil];
[shoppingItem retain];
[cartInstance addObject:shoppingItem];
id obj;
NSEnumerator * enumerator = [cartInstance objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
}
else{
NSLog(@"Error: Could not add article to shoppingcart.");
}
}
Can anyone help me out? Thanks in advance.
One problem you've got is here:
newAmount = [NSNumber numberWithInt:tempInt];
[[cartInstance objectAtIndex:i] setObject:newAmount forKey:amount];
[newAmount release];
This allocates an autoreleased NSNumber, but you release it manually later. Don't do this.
Try using "Build & Analyze" on your app; it'll point you to memory management problems like this.
In short, don't release anything that you didn't allocate. Since you didn't call "alloc" for newAmount. You shouldn't call release as well.
精彩评论