我已经将头撞在显示器上几个小时(并阅读了类似的帖子)。我仍然很难过。
我在 *.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];
}
在我的新手状态下,我认为这[teamsArray release]
会释放字典对象。我还尝试使用“alloc] init]”来创建字典,以及释放和重新分配teamArray(而不是调用removeAllObjects
)。