我開發了一個應用程序,現在我使用儀器來查看內存使用情況。我在理解對象的保留/釋放過程時遇到問題。這是儀器說什麼:iOS - 內存和保留/釋放問題
http://f.cl.ly/items/1Y2Z1e2K3e3Y3e1I1z26/Schermata%202012-12-19%20alle%2011.18.51.png
保留計數增加,當添加我的對象到一個數組,當我將其添加在我看來,當我從陣列中起飛。 因此,當我使用removeFromSuperview
時,對象retainCount
將永遠不會爲零,因此對象不會釋放內存。編輯1: 我忘了說我正在使用ARC。
編輯2: 我描述正是發生: 我在一個叫NKLevelGenerator
類一起創建對象的其他對象。進入它,我分配NKIngredient
s,然後我將全部添加到將返回的NSArray
。這裏的每一個對象的保留數爲2。在我NKLevelVC
,我ViewController
,我用這個指令:
[level addObjectsFromArray:[levelGenerator level1WithDelegate:self ciotola:ciotola bagliore:bagliore difficulty:NKDifficultyHard]];
對象level
是NSMutableArray
我alloc
和viewDidLoad
init
。 在這裏,我呼籲執行此操作的另一種方法:
- (void)insertInArrayRandomly {
for (int i=0; i<[level count]; i++) {
[ingredienti insertObject:[level objectAtIndex:[[indexes objectAtIndex:i]integerValue]] atIndex:i];
}
}
對象ingredienti
是另一個NSMutableArray
我alloc
和viewDidLoad
init
。 indexes
是一個NSInteger
的數組,其中包含隨機索引以隨機提取NKIngredient
對象。 那麼我這樣做:
NKIngredient *ing = [ingredienti objectAtIndex:index];
[[self view] insertSubview:ing belowSubview:navBar];
[ing animateIngredient];
[ingredienti removeObject:ing];
以下是圖像:http://f.cl.ly/items/1Y2Z1e2K3e3Y3e1I1z26/Schermata%202012-12-19%20alle%2011.18.51。png –