有人能請指教爲什麼我在這段代碼中有內存泄漏?內存泄漏問題
我在代碼中添加了分析器中的註釋。如果有人能夠幫助我並解釋爲什麼我會收到這兩條評論,我將不勝感激。
- (void)viewDidDisappear:(BOOL)animated {
// Empty array to be sure it is empty
[playerArray removeAllObjects];
//============CLEAN OUT DOUBLE NAMES FROM ARRAY==============//
NSArray *sortedDummyArray = [[NSArray alloc] initWithArray:selectedPlayersArray];
////>>>>The line above is line 84<<<<<<<////
// Sort the array
sortedDummyArray = [sortedDummyArray sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *finalArray = [[NSMutableArray alloc]initWithArray:sortedDummyArray];
////>>>>>> Possible memory leak on line 84 <<<<<<<<////
int xx = [sortedDummyArray count];
int yy;
int counter = 0;
int rr = 0;
for (int oo = 0; oo < xx; oo++) {
yy = [finalArray count];
for (int zz = 0; zz < yy; zz++) {
// If hit, clean out the double name
if ([[sortedDummyArray objectAtIndex:oo] isEqualToString:[finalArray objectAtIndex:rr]]) {
counter++;
// Check if there is more than one of this name
if (counter > 1) {
[finalArray removeObjectAtIndex:rr];
rr--;
counter--;
}
}
rr++;
}
counter = 0;
rr = 0;
}
[sortedDummyArray retain];
// Save who is in the game
AccessQuestionsDB *shufflePlayersFunction = [AccessQuestionsDB new];
finalArray = [shufflePlayersFunction shufflePlayers: finalArray];
[shufflePlayersFunction release];
TempPlayersInTheGame *savePlayersInTheGame = [TempPlayersInTheGame new];
[savePlayersInTheGame saveSelectedPlayers:finalArray];
[savePlayersInTheGame release];
[finalArray release]; //>>>> see comment below
////>>>>>Incorrect decrement of the reference count of an object that is not owned at this point by the caller <<<<<</////
[sortedDummyArray release];
[super viewDidDisappear:animated];
}
我仍然會避免在不同的時間使用相同的指針指向2個不同的數組。而不是在接近尾聲時重新設置finalArray;我會有一個完全不同的指針用於savePlayersInTheGame。只是爲了保持簡單。 – GendoIkari 2011-03-15 21:03:47
A BIG謝謝你,這個論壇對待像我這樣的新人真的很棒:-) – PeterK 2011-03-15 21:09:37
@Gendolkari我同意,我清理了內存泄漏以幫助說明是什麼導致了他們。但對於這個特殊的問題(刪除重複的名稱),我發佈了一個更簡單的解決方案,它可以消除所有的泄漏和指針重用。 – theChrisKent 2011-03-15 21:15:00