Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 目标C泄漏问题_Iphone_Objective C_Memory Leaks - Fatal编程技术网

Iphone 目标C泄漏问题

Iphone 目标C泄漏问题,iphone,objective-c,memory-leaks,Iphone,Objective C,Memory Leaks,根据仪器,我在下面的代码中有漏洞。有没有人能给我一些建议和解释,为什么我会在这些线路上得到指示 以下管线标记为泄漏: NSMutableArray *read_Question = [[NSMutableArray alloc] initWithCapacity: 0]; 下面是完整的代码: - (NSMutableArray *)readQuestion: (int)questionNr { NSMutableArray *read_Question = [[NSMutableArra

根据仪器,我在下面的代码中有漏洞。有没有人能给我一些建议和解释,为什么我会在这些线路上得到指示

以下管线标记为泄漏:

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];
[read_Question autorelease];

return read_Question;
}

这是分配对象的地方,但不是泄漏对象的地方。仪器可以告诉对象何时分配,以及在何处保留和释放,但它不知道哪个释放对应于哪个保留,因此它将泄漏归因于初始分配


查找这些对象的使用位置。仪器中有一个视图可以显示一个街区的历史,但是你最好还是仔细想想。什么代码保留这些对象?你能证明相同的代码在所有情况下都会释放它们吗?

你是在自动释放池中自动释放的吗?i、 e.您是否在调用
-read_Question
的线程中创建了
NSAutoreleasePool
的实例?我假设这是在主线程中调用的,默认情况下,
main.m
文件如下所示:

int main(int argc, char* argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool drain];
    return retVal;
}
我不认为这是你的问题,但在没有完整细节的情况下,我还是要说——如果另一个线程调用了
-read\u Question
,你需要类似的东西:

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    .
    .
    .
[self read_Question];
    .
    .
    .
[pool drain];
否则,请参见@Steven的建议

int main(int argc, char* argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool drain];
    return retVal;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    .
    .
    .
[self read_Question];
    .
    .
    .
[pool drain];