Iphone 从表视图中删除行时内存泄漏

Iphone 从表视图中删除行时内存泄漏,iphone,objective-c,cocoa-touch,memory-leaks,Iphone,Objective C,Cocoa Touch,Memory Leaks,我正在将数据从plist加载到应用程序中的tableview中。数据存储在可变字典的可变字典中。 以下是我的viewDidLoad方法: - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Categories"; // load data from plist fle self.categories = [[[NSMutableDictionary alloc] initWithContentsO

我正在将数据从plist加载到应用程序中的tableview中。数据存储在可变字典的可变字典中。 以下是我的viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Categories";

    // load data from plist fle
    self.categories = [[[NSMutableDictionary alloc] initWithContentsOfFile:
                                  [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]] autorelease];

    // add buttons to navigation menu
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.leftBarButtonItem = self.addButton;
}
我的tableview是可编辑的,因此用户可以删除类别。在我的CommittedItingStyle:forRowAtIndexPath:method中,我更新数据模型:

[self.categories removeObjectForKey: [[self.categories allKeys] objectAtIndex:indexPath.row]];
当我配置我的应用程序时,它会泄漏内存。我不太擅长使用profile工具,但每次删除一行时,它似乎都会在我的分类词典中发现漏洞

我想知道我在哪里错过了一些东西?我要删除的对象也是一个字典,并且我也需要删除它的对象,这是一个问题吗?

这会泄漏(如果属性是retain或copy):

改用这个:

categories = [[NSMutableDictionary alloc] initWithContentsOfFile:
                                  [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]];
或者这个:

self.categories = [[[NSMutableDictionary alloc] initWithContentsOfFile:
                                  [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]] autorelease];
此漏洞(如果属性为保留或复制):

改用这个:

categories = [[NSMutableDictionary alloc] initWithContentsOfFile:
                                  [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]];
或者这个:

self.categories = [[[NSMutableDictionary alloc] initWithContentsOfFile:
                                  [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]] autorelease];

我想知道这是否会给你带来什么不同

NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithContentsOfFile:
                                  [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]];

self.categories = d;
[d release];

我想知道这是否会给你带来什么不同

NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithContentsOfFile:
                                  [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]];

self.categories = d;
[d release];

泄漏背后的原因是init创建了一个1的保留计数,而属性上的retain也创建了一个1的保留计数,当您只期望1时,它将为您提供一个2的保留计数。泄漏。谢谢你的快速回答!我为self.categories添加了自动释放,但仍然存在相同的问题。泄漏的对象包括:\ NSCFDictionary和NSCFString。@Shvetusya您已经向我们展示了如何创建字典以及如何从字典中删除对象,但在这两者之间,您必须使用这些对象在表视图中显示数据。也许你们保留了那个些东西(只是猜测而已),不得不重新开始这个项目,但最终还是找到了答案@是的,问题出在代码之间;每个类别都有条目字典,我没有正确地释放loc items属性。谢谢你的帮助!泄漏背后的原因是init创建了一个1的保留计数,而属性上的retain也创建了一个1的保留计数,当您只期望1时,它将为您提供一个2的保留计数。泄漏。谢谢你的快速回答!我为self.categories添加了自动释放,但仍然存在相同的问题。泄漏的对象包括:\ NSCFDictionary和NSCFString。@Shvetusya您已经向我们展示了如何创建字典以及如何从字典中删除对象,但在这两者之间,您必须使用这些对象在表视图中显示数据。也许你们保留了那个些东西(只是猜测而已),不得不重新开始这个项目,但最终还是找到了答案@是的,问题出在代码之间;每个类别都有条目字典,我没有正确地释放loc items属性。谢谢你的帮助!