簡體   English   中英

為什么是executeFetchRequest:fetchRequest泄漏內存?

[英]Why is executeFetchRequest:fetchRequest leaking memory?

儀器顯示以下代碼泄漏,如果我注釋掉這段代碼沒有泄漏。

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和/或塊的泄漏。 任何幫助將不勝感激,並提前感謝。

它似乎是executeFetchRequest:onSuccess:onFailure:是您在NSManagedObjectContext類別中定義的函數。 確保要傳遞給onSuccess塊的NSArray對象實例是自動釋放的。

事實上,事實證明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]];`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM