【问题标题】:Memory Leak with NSFetchedResultsController objectAtIndex内存泄漏与 NSFetchedResultsController objectAtIndex
【发布时间】:2011-03-14 01:42:26
【问题描述】:

我在使用 NSFetchedResultsController objectAtIndex 时出现内存泄漏: 栈帧如下:

   0 CoreFoundation __CFDataAllocate
   1 CoreFoundation __CFDataInit
   2 CoreData -[NSSQLCore _prepareResultsFromResultSet:usingFetchPlan:withMatchingRows:]
   3 CoreData -[NSSQLCore _newRowsForFetchPlan:selectedBy:withArgument:]
   4 CoreData -[NSSQLCore newRowsForFetchPlan:]
   5 CoreData -[NSSQLCore objectsForFetchRequest:inContext:]
   6 CoreData -[NSSQLCore executeRequest:withContext:error:]
   7 CoreData -[NSPersistentStoreCoordinator executeRequest:withContext:error:]
   8 CoreData -[NSManagedObjectContext executeFetchRequest:error:]
   9 CoreData _faultBatchAtIndex
  10 CoreData -[_PFBatchFaultingArray objectAtIndex:]
  --->  11 MyApp -[DocumentViewController tableView:heightForRowAtIndexPath:] 
  12 UIKit -[UISectionRowData refreshWithSection:tableView:tableViewRowData:]
  13 UIKit -[UITableViewRowData rectForFooterInSection:]
  14 UIKit -[UITableViewRowData heightForTable]
  15 UIKit -[UITableView(_UITableViewPrivate) _updateContentSize]
  16 UIKit -[UITableView noteNumberOfRowsChanged]
  17 UIKit -[UITableView reloadData]
  18 UIKit -[UITableView layoutSubviews]
  19 QuartzCore -[CALayer layoutSublayers]
  20 QuartzCore CALayerLayoutIfNeeded
  21 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
  22 QuartzCore CA::Transaction::commit()
  23 QuartzCore CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)
  24 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
  25 CoreFoundation __CFRunLoopDoObservers
  26 CoreFoundation __CFRunLoopRun
  27 CoreFoundation CFRunLoopRunSpecific
  28 CoreFoundation CFRunLoopRunInMode
  29 GraphicsServices GSEventRunModal
  30 GraphicsServices GSEventRun
  31 UIKit UIApplicationMain

从这个堆栈帧来看,使用 objectAtIndex 访问的对象似乎泄露了。

但是,当我离开控制器时,它的 dealloc 方法被调用。在这种方法中,由于我使用 [NSManagedObjectContext refreshObject:mergeChange],fetchedresultscontroller 获取的所有对象都变成了故障。

为了在我的描述中完整,下面是创建泄漏对象的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Page* page = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    UIImage* thumbnail = [page getThumbnail];
    [[self managedObjectContext] refreshObject:page.image mergeChanges:NO];
    CGFloat height = [PageCell cellImageSize:thumbnail].height + 20;
    return height;
}

任何提示将不胜感激, 谢谢!

【问题讨论】:

    标签: iphone cocoa core-data memory-leaks


    【解决方案1】:

    检查您获得UIImage 的代码。泄漏很可能在那里。它在哪里完全取决于您如何创建图像对象。

    【讨论】:

      【解决方案2】:

      除非有更多证据,否则这看起来更像是 Core Data 内部的漏洞。提交错误。

      【讨论】:

      • 有趣的一点:如果我向模拟器发送“内存不足警告”,对象就会消失。那么它们会是某种缓存吗?
      猜你喜欢
      • 1970-01-01
      • 2011-10-16
      • 2019-08-08
      • 2012-08-21
      • 2016-01-25
      • 1970-01-01
      • 2015-04-15
      • 2014-03-26
      • 2012-07-09
      相关资源
      最近更新 更多