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;

}

那么我对导致这种情况的核心数据有什么问题吗?关于如何停止堆增长的任何建议?

4

1 回答 1

0

一切看起来都很好。内存的最小增加可以认为是正常的,应该是可以预期的。

于 2012-12-20T11:55:55.583 回答