2011-06-24 54 views
0

我有泄漏問題。在這種editingSessionArray 下面這一行:實例中的內存泄漏方法目標

NSMutableArray *sessionsArray = [[NSMutableArray alloc] initWithArray:[BBWorkoutSession fetchSessionDetail1:selectedWorkoutId]]; 

說方法返回一個Objective-C的對象與+1保留計數(由於參考):

是沒有我所犯的錯誤。

請幫我

-(void)editingSessionsArray 
{ 
    [[BBAppController sharedInstance] updateLogWithString:[NSString stringWithFormat:@"BEGIN : %s", __PRETTY_FUNCTION__]]; 
    if(selectedWorkoutId) { 
    // NSLog(@"the section id is : %@",selectedWorkoutId); 
    NSMutableArray *sessionsArray = [[NSMutableArray alloc] initWithArray:[BBWorkoutSession fetchSessionDetail1:selectedWorkoutId]]; 

    /* condition for checking the selected workout id is existing or not */ 

    if ([workOutSessionData count]) { 
     for (int i= 0; i< [workOutSessionData count] ; i++) { 
      if ([[[workOutSessionData objectAtIndex:i] allKeys] containsObject:[NSString stringWithFormat:@"%@",selectedWorkoutId]]) { 
       [workOutSessionData removeObjectAtIndex:i]; 

       return; 
      } 
     } 
    } 

    /* If console comes here the workout id is not existed in the main array */ 


    [mainDict setValue:sessionsArray forKey:[NSString stringWithFormat:@"%@",selectedWorkoutId]]; 

    NSMutableDictionary *workoutsList = [[NSMutableDictionary alloc]initWithObjectsAndKeys:sessionsArray,[NSString stringWithFormat:@"%@",selectedWorkoutId],nil] ; 
    [workOutSessionData addObject:workoutsList]; 
    [workoutsList release]; 
    [sessionsArray release]; 
    } 
    [[BBAppController sharedInstance] updateLogWithString:[NSString stringWithFormat:@"END : %s", __PRETTY_FUNCTION__]]; 
} 

回答

4

如果你的代碼放在這裏:

if ([workOutSessionData count]) { 
for (int i= 0; i< [workOutSessionData count] ; i++) { 
    if ([[[workOutSessionData objectAtIndex:i] allKeys] containsObject:[NSString stringWithFormat:@"%@",selectedWorkoutId]]) { 
     [workOutSessionData removeObjectAtIndex:i]; 

     return; 
    } 
} 

}

你回不釋放workoutList和sessionArray。 你應該分配你的session數組與autorelease

NSMutableArray *sessionsArray = [[[NSMutableArray alloc] initWithArray:[BBWorkoutSession fetchSessionDetail1:selectedWorkoutId]] autorelease]; 
+0

+ +1爲一個好眼睛。看着它2分鐘,沒有看到。 – Cyprian