Ram
Ram

Reputation: 2513

How to release an locally created object, while it using as return object

In my pgm, i am getting a leak warning in this segment.

-(NSMutableArray *)filterArrayForBank:(NSMutableArray *)originalArray withKey:(NSString *)key{ NSMutableArray *mutableArray=[[NSMutableArray alloc]init];

for (int i=0; i<[originalArray count]>0; i++) {
    if([[[originalArray objectAtIndex:i]objectForKey:@"transType"] isEqualToString:key]){
        [mutableArray addObject:[originalArray objectAtIndex:i]];
    }
}
//////NSLog(@"mutableArray %@",mutableArray);
       return mutableArray ;

}

if i block this leak by below line, app get crash

either return [mutableArray autorelease];

or

NSMutableArray *mutableArray=[[[NSMutableArray alloc]init]autorelease];

plz help me to stop this leak. thanks in advance.

Upvotes: 0

Views: 139

Answers (1)

hwaxxer
hwaxxer

Reputation: 3383

If you return an autoreleased array you need to make sure you retain it in the caller method. However, if you are not retaining it in the caller method, try renaming the method to:

-(NSMutableArray *)newFilterArrayForBank:(NSMutableArray *)originalArray withKey:(NSString *)key

This will notify the compiler that you are allocating a new NSMutableArray.

Upvotes: 1

Related Questions