“-[CFNumber intValue]:发送到已释放实例的消息”不知道
我有一个exc访问不良的问题。我已经启用了NSZombieEnabled,但不明白为什么会导致这个问题。 您可以在下面的函数中看到 cartInstance 数组是如何定义的。 它是一个带有多个 NSMutableDictionaries 的 NSMutableArray 每当我的计数器 i 达到 13 时,就会发生错误。 然后我收到一个错误的访问和如标题所示的消息。 这是一些代码:
-(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] objectForKey: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.");
}
}
任何人都可以帮助我吗? 提前致谢。
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] objectForKey: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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到的一个问题是:
这分配了一个自动释放的 NSNumber,但您稍后手动释放它。不要这样做。
尝试在您的应用程序上使用“构建和分析”;它会向您指出这样的内存管理问题。
One problem you've got is here:
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.
简而言之,不要释放任何未分配的内容。因为您没有为 newAmount 调用“alloc”。您也不应该调用释放。
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.