为什么是executeFetchRequest:fetchRequest泄漏内存?

时间:2013-06-16 06:27:05

标签: iphone ios objective-c objective-c-blocks stackmob

仪器显示以下代码泄漏,如果我注释掉此代码,则没有泄漏。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    // Edit the entity name as appropriate.

    NSEntityDescription *entity = [NSEntityDescription entityForName:USER_CORE_DATA inManagedObjectContext:self.managedObjectContext];

    [fetchRequest setEntity:entity];

    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"username == %@", [[User defaultManager] savedUsername]];
    [fetchRequest setPredicate:predicte];

    // set any predicates or sort descriptors, etc.

    // execute the request
    [self.managedObjectContext executeFetchRequest:fetchRequest onSuccess:^(NSArray *results) {

    } onFailure:^(NSError *error) {

        NSLog(@"Error fetching: %@", error);

    }];
    [fetchRequest release];

具体来说,乐器在上面的代码中说了这一行:

[self.managedObjectContext executeFetchRequest:fetchRequest onSuccess:^(NSArray *results)

它似乎是fetchRequest和/或块的泄漏。任何帮助将不胜感激,并提前感谢。

2 个答案:

答案 0 :(得分:0)

看起来executeFetchRequest:onSuccess:onFailure:是您在NSManagedObjectContext类别中定义的函数。确保传递给onSuccess块的NSArray对象实例已自动释放。

答案 1 :(得分:0)

实际上,事实证明StackMob的代码有泄漏,我在那里下载了源码并修复了它。

- (NSString *)primaryKeyField
{
    NSString *objectIdField = nil;

    // Search for schemanameId
    objectIdField = [[self SMSchema] stringByAppendingFormat:@"Id"];
    if ([[[self entity] propertiesByName] objectForKey:objectIdField] != nil) {
        return objectIdField;
    }

    objectIdField = nil;  // This line was missing and causing a leak

    // Search for schemaname_id
    objectIdField = [[self SMSchema] stringByAppendingFormat:@"_id"];
    if ([[[self entity] propertiesByName] objectForKey:objectIdField] != nil) {
        return objectIdField;
    }

    objectIdField = nil;  // This line was missing and causing a leak

    // Raise an exception and return nil
    [NSException raise:SMExceptionIncompatibleObject format:@"No Attribute found for `entity %@ which maps to the primary key on StackMob. The Attribute name should match one of the following formats: lowercasedEntityNameId or lowercasedEntityName_id.  If the managed object subclass for %@ inherits from SMUserManagedObject, meaning it is intended to define user objects, you may return either of the above formats or whatever lowercase string with optional underscores matches the primary key field on StackMob.", [[self entity] name], [[self entity] name]];`