简体   繁体   English

Objective-C泄漏问题

[英]Objective-C leak problem

According to the Instruments I have leaks in the code below. 根据仪器,我在下面的代码中有泄漏。 Any nice person that can give me some advice and explanation why i get indications on these lines? 有谁能给我一些建议和解释,为什么我会在这些方面得到适应症?

The following lines are tagged as leaks: 以下几行被标记为泄漏:

NSMutableArray *read_Question = [[NSMutableArray alloc] initWithCapacity: 0];

NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];

if ([[info valueForKey:@"idQ"] intValue] == questionNr) { 
        [read_Question addObject:[info valueForKey:@"question"]];

So here is the full code: 所以这是完整的代码:

- (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;
}

That's where the objects are allocated, but it's not where they're being leaked. 那是分配对象的地方,但不是泄漏对象的地方。 Instruments can tell when an object is allocated, and where it's retained and released, but it has no idea which release corresponds to which retain so it attributes the leak to the initial allocation. 仪器可以告诉对象何时分配以及在何处保留和释放对象,但是不知道哪个释放对应于哪个保留,因此它将泄漏归因于初始分配。

Look for where these objects are used. 查找这些对象的使用位置。 There's a view in Instruments for showing the history of a block, but you're probably better off thinking this through instead. Instruments中有一个视图可以显示块的历史记录,但是最好还是仔细考虑一下。 What code retains these objects? 哪些代码保留了这些对象? Can you prove that the same code releases them in all cases? 您能证明在所有情况下都可以释放相同的代码吗?

Are you autoreleasing inside an autorelease pool? 您是否在自动释放池中自动释放? ie Have you created an instance of NSAutoreleasePool in the thread calling -read_Question ? 即您是否在调用-read_Question的线程中创建了NSAutoreleasePool的实例? I'm assuming this is being called in the main thread, and your main.m file will by default look like: 我假设这在主线程中被调用,并且您的main.m文件默认情况下将如下所示:

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

I don't think this is your problem, but without having the full details I'll say this anyway -- If -read_Question is being called in another thread, you need something similar: 我不认为这是您的问题,但是如果没有全部详细信息,我还是会这样说-如果在另一个线程中调用-read_Question则需要类似的内容:

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    .
    .
    .
[self read_Question];
    .
    .
    .
[pool drain];

Otherwise, see @Steven's suggestion! 否则,请参阅@Steven的建议!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM