我已经把我的头撞在监视器上好几个小时了(并且阅读了类似的帖子)。我仍然难倒。还有另一个泄漏的NSMutableDictionary Post
我声明在我* h文件的数组,将在表视图中显示数据:
@interface TeamsTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *teamsArray;
}
我分配在阵列中-viewDidLoad
并释放它在的dealloc:
-(void)viewDidLoad
{
[super viewDidLoad];
// Inititalize the mutablearray that will store the xml for the table.
teamsArray = [[NSMutableArray alloc] init];
}
-(void)dealloc
{
[teamsArray release];
[super dealloc];
}
每个时间-viewWillAppear
,我打电话给我的loadData重新加载数据(因为数据可以改变)。对于这篇文章(当我试图找到我的泄漏),我已经硬编码数据。该评论显示了所报告的泄漏的位置。 (当表视图重新显示发生了泄漏。)
-(void)loadData
{
// Empty any objects that are already in the array.
[teamsArray removeAllObjects];
// Fill a dictionary (normally looping through a file, but hardcoded for leak hunting).
NSMutableDictionary *oneTeamDictionary = [NSMutableDictionary dictionary];
[oneTeamDictionary setObject:@"100" forKey:@"basenumber"];
[oneTeamDictionary setObject:@"Team Name" forKey:@"teamname"];
[oneTeamDictionary setObject:@"XYZ" forKey:@"teamabbr"];
[oneTeamDictionary setObject:@"USA" forKey:@"countryabbr"];
[oneTeamDictionary setObject:@"Joe" forKey:@"manager"];
// Add this team to the array used to display table data.
[teamsArray addObject:[oneTeamDictionary copy]]; // Leaks Malloc 32 bytes and _NSCFDictionary 48 bytes here.
// Reload the table view with new data.
[self.tableView reloadData];
}
在我newbieistic状态,我会认为[teamsArray release]
将释放的字典对象。我也尝试使用“alloc] init]”创建字典,以及释放和重新分配teamsArray(而不是调用)。
如果你在viewDidLoad中分配对象,你应该在viewDidUnload中释放它们。 viewDidLoad和viewDidUnload可以在同一个对象上多次调用。 – Marcio 2011-02-10 14:13:08