需要帮助解决内存泄漏问题

时间:2011-06-02 06:05:28

标签: iphone objective-c core-data memory-leaks

我刚刚为我的应用运行Intrument的内存泄漏工具,我收到以下错误:

enter image description here

以下是它所指的方法:

此行明确说明:NSSet *filteredExercisesFromSession = [session.exercises filteredSetUsingPredicate:[NSPredicate predicateWithFormat: @"name == %@", selectedExerciseName]]

- (void)createSession
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *dateComponentsForToday = [calendar components:unitFlags fromDate:self.picker.date];
    [dateComponentsForToday setHour:0];
    [dateComponentsForToday setMinute:0];
    [dateComponentsForToday setSecond:0];
    NSDate *targetDateBegins = [calendar dateFromComponents:dateComponentsForToday]; 
    NSDate *targetDateEnds = [targetDateBegins dateByAddingTimeInterval:(60 * 60 * 24 - 1)];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"(timeStamp >= %@ && timeStamp <= %@)", targetDateBegins, targetDateEnds]];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:managedObjectContext]];

    NSError *error = nil;
    NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    NSLog(@"Fetch error: %@", error);

    if ([results count])
    {
        session = (Session *)[results objectAtIndex:0];
        NSLog(@"Already have session for date: %@", session.timeStamp);
    }
    else
    {
        session = (Session *)[NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:managedObjectContext];
        session.timeStamp = self.picker.date;
        NSLog(@"New session for date: %@", session.timeStamp);
    }

    NSSet *filteredExercisesFromSession = [session.exercises filteredSetUsingPredicate:[NSPredicate predicateWithFormat: @"name == %@", selectedExerciseName]];
    if ([filteredExercisesFromSession count] > 0)
    {
        self.exercise = [filteredExercisesFromSession anyObject];
    }
    else
    {
        NSLog(@"exercise does not already exist");
        self.exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
        self.exercise.name = selectedExerciseName;
        [session addExercisesObject:exercise];
        NSLog(@"exercise name: %@", exercise.name);
        NSLog(@"exercises in session:  %@",session.exercises);
    }

    if (![managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
    NSLog(@"Save error: %@", error);

    [fetchRequest release];
    self.fetchedResultsController = nil;
    [setsTableView reloadData];
}

1 个答案:

答案 0 :(得分:0)

您的代码具有self.exercise =,它取得所有权,因此请确保在dealloc中释放支持exercise属性的成员变量。