简体   繁体   中英

Memory Leak iOS

What cuases my memory leak here:

I have global variable:

@property (nonatomic, strong) NSArray *productArray;

I have function implementation that query data from core data:

- (NSArray *)fetchallProductWithTag:(NSString *)tag
{
   NSPredicate *predicate = 
     [NSPredicate predicateWithFormat:@"tags.name contains [cd] %@", tag];

   NSSet *itemsSet = [self.managedObjectContext        
         fetchObjectsForEntityName:TABLE_NAME_PRODUCT 
                     withPredicate:predicate 
                           columns:nil unique:NO];

    return itemsSet.allObjects;
}

Here is the implementation of fetchObjectsForEntityName:withPredicate:columns: from a category class:

- (NSSet *)fetchObjectsForEntityName:(NSString *)entityName
                   withPredicate:(NSPredicate *)predicate
                         columns:(NSArray *)columns
                          unique:(BOOL)unique
  {
      NSEntityDescription *entity = [NSEntityDescription
            entityForName:entityName inManagedObjectContext:self];

      NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

     [request setEntity:entity];
     [request setPredicate:predicate];
     [request setReturnsDistinctResults:unique];

     if( columns.count > 0)
        [request setPropertiesToFetch:columns];

     if( columns.count > 0 || unique )
        [request setResultType:NSDictionaryResultType];

     NSError *error = nil;

     NSArray *results = [self executeFetchRequest:request error:&error];

     if (error != nil)
     {
          [NSException raise:NSGenericException 
               format:@"Error fetching in %@; error:%@", 
               entityName, error.localizedDescription];
     }

    if( results.count > 0 )
    {
        return [NSSet setWithArray:results];
    }
    return nil;
}

In my view controller I have this function call:

self.productArray = [myClass fetchAllProductWithTag:@"All"];

Then somewhere in viewcontroller class code I reset the value of productArray:

self.productArray = [myClass fetchAllProductWithTag:@"Favorites"];

Then the leak happens.

It turned out that the line causing the leak was try-catch statement. I had something like this:

Product *product = nil;

@try 
{
   product = [self.productArray objectAtIndex:index];
}
@catch (NSException *exception) 
{
   return;        
}

I didn't want to check if the index was out of bound. So I put it in a try-catch and return if an exception occur.

So, I tried to remove the try-catch and had something like this:

Product *product = nil;

if( index < self.productArray.count )
  product = [self.productArray objectAtIndex:index]
else 
  return;

Finally, the leak was gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM