内存泄漏问题,我需要帮助#1
|
我对此非常陌生,似乎在这段我无法修复的代码中存在漏洞:
仪器在此行显示100%:
NSMutableArray * read_Question = [[[NSMutableArray alloc] initWithCapacity:0];
我已经尝试过各种方法,但无法修复。
有人可以建议我如何进行吗?
- (NSMutableArray *)readQuestion: (int)questionNr {
NSMutableArray *read_Question = [[NSMutableArray alloc] initWithCapacity: 0];
NSError *error;
//=========PREPARE CORE DATA DB===========//
if (managedObjectContext == nil) {
managedObjectContext = [(FamQuiz_R0_1AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; }
// Define qContext
NSManagedObjectContext *qContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@\"questions\" inManagedObjectContext:qContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
if ([[info valueForKey:@\"idQ\"] intValue] == questionNr) {
[read_Question addObject:[info valueForKey:@\"question\"]];
[read_Question addObject:[info valueForKey:@\"qRightAnswer\"]];
[read_Question addObject:[info valueForKey:@\"qWrongAnswer1\"]];
[read_Question addObject:[info valueForKey:@\"qWrongAnswer2\"]];
}
}
[fetchRequest release];
return [read_Question autorelease];
}
没有找到相关结果
已邀请:
5 个回复
搜洼挂时
超可林
搂腹时
脾萌
然后重新添加您的发行版。
门锑腺潮
仅当retainCount达到0时,才使用retain,alloc和copy释放对象,从而增加retainCount。请记住:仅当使用访问器方法时,保留才真正发生(除了使用alloc,保留和直接复制外)。使用以下方法之一时,通常会调用accessor方法:
您已经在代码中创建了一个keep属性,但是您并未使用accessor方法,因此该对象从未被保留。
如果您从这一点开始释放它,将导致崩溃,因为retainCount实际上在某个时刻变为-1(因为它从一开始就从未达到1)。因此,您应该这样设置属性:
或者(我认为最好):
这样,您可以确定保留确实发生了。 在我看来,访问器设置程序的第二种表示法是高级的,我认为在可能的情况下使用它来设置属性是个好习惯。在以下站点上了解有关分享此观点及其推理的人们的更多信息: 可可是我的女朋友 大书呆子牧场