3

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

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 和/或块的泄漏。任何帮助将不胜感激,并提前致谢。

4

2 回答 2

0

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

于 2013-06-16T06:53:17.113 回答
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]];`
于 2013-06-19T05:57:27.037 回答