“-[CFNumber intValue]:发送到已释放实例的消息”不知道

发布于 2024-08-24 09:27:36 字数 1853 浏览 8 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2024-08-31 09:27:36

您遇到的一个问题是:

            newAmount = [NSNumber numberWithInt:tempInt];
            [[cartInstance objectAtIndex:i] setObject:newAmount forKey:amount];
            [newAmount release];

这分配了一个自动释放的 NSNumber,但您稍后手动释放它。不要这样做。

尝试在您的应用程序上使用“构建和分析”;它会向您指出这样的内存管理问题。

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.

臻嫒无言 2024-08-31 09:27:36

简而言之,不要释放任何未分配的内容。因为您没有为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文