0
當我運行儀器時,我注意到一些堆增長。沒有泄漏,但是有一些內存不清除,這看起來是由於我使用核心數據造成的。停止與核心數據管理對象相關的堆增長
從堆棧跟蹤似乎增長髮生在我保存的管理對象。
這是我如何創建管理對象:
ScramblerGame *game = (ScramblerGame *)[NSEntityDescription insertNewObjectForEntityForName:@"ScramblerGame" inManagedObjectContext:self.context];
game.time = [NSNumber numberWithInt:self.time];
game.score = 0; //etc...
ScramblerGame是沒有修改數據模型正確創建的管理對象子類。
這是我如何訪問和更新管理對象:
self.game.score = [NSNumber numberWithInt:[self.game.score intValue] + score];
self.game
是另一個類的管理對象弱引用。
這是在遊戲結束時調用的方法,用於保存或刪除數據。這也是我在引用圖像的堆棧跟蹤中調用的最後一個方法,這會導致堆增長。
-(void)saveAndHandleGameData:(BOOL)stillPlaying{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextObjectsDidChangeNotification object:nil];
//save the data to disk
self.game.inProgress = [NSNumber numberWithBool:stillPlaying];
SCAppDelegate *appDelegate = ((SCAppDelegate *)[[UIApplication sharedApplication] delegate]);
NSManagedObjectContext *context = appDelegate.managedObjectContext;
if([self.game.score intValue] == 0)
[context deleteObject:self.game];
[context save:nil];
self.game = nil;
}
所以我在做什麼錯誤的核心數據造成這種情況?有關如何阻止堆增長的任何建議?