简体   繁体   English

内存泄漏iOS

[英]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: 这是来自类别类的fetchObjectsForEntityName:withPredicate:columns:的实现:

- (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: 然后在viewcontroller类代码中的某处,我重置productArray的值:

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

Then the leak happens. 然后发生泄漏。

It turned out that the line causing the leak was try-catch statement. 原来,导致泄漏的行是try-catch语句。 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. 因此,我将其放入try-catch并在发生异常时返回。

So, I tried to remove the try-catch and had something like this: 因此,我尝试删除try-catch并进行如下操作:

Product *product = nil;

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

Finally, the leak was gone. 终于,泄漏消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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