开发者

Problem Memory Leak in Core Data code

I can't seem to figure out the cause of a problem reported by the Leaks tool. The code in question is here with the problem line marked. Any ideas? Is there a way to find out exactly what object is the leak?

NSArray *getAllCoreData(NSString *entityName, NSString *orderedBy, BOOL ascending, BOOL shallow)
{
// Get the managed object context
NSManagedObjectContext *moc = [[AppController sharedAppController] managedObjectContext];

// Create a fetch request that fetches from 'entityName'
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:moc];
[fetch setEntity:entity];

// Try to do the fetch
NSError *error;
NSArray *r开发者_开发百科esult = [moc executeFetchRequest:fetch error:&error]; <----- Problem line

[fetch release];

// Did the fetch fail?
if (!result)
{
    // Display an alert view
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Fetch Failed"
                                                        message:[error localizedDescription] 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];

    [alertView autorelease];
    [alertView show];
    return nil;
}

// Return the array of instances of NSManagedObject
return result;
}

Any help will be greatly appreciated, Jason


Leaks is telling you the memory allocated there was kept around after you had no more references.

So after you return results, something else is retaining it too long, and not releasing when you are done.


Try changing

NSError *error;

to

NSError *error = nil;

It may not be a real leak. Possibly, NSError *error just happens to have some leftover pointer in it so it looks like a leak to the tool.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜