我已經把我的頭撞在監視器上好幾個小時了(並且閱讀了類似的帖子)。我仍然難倒。還有另一個泄漏的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