Note that there are some explanatory texts on larger screens.

plurals
  1. POself.tableView.delegate = self and self.table.dataSource = self memory leak
    text
    copied!<p>I have following codes, which have memory leak on device, could you please kindly help check it? Thanks.</p> <pre><code>@interface NewsListViewController : UIViewController&lt;UITableViewDataSource, UITableViewDelegate&gt; { @private UITableView *tableView; NSFetchedResultsController *fetchedResultsController; ...... } @property (nonatomic, retain, readonly) NSFetchedResultsController *fetchedResultsController; @end @implementation NewsListViewController { ...... - (void)dealloc { [fetchedResultsController release]; fetchedResultsController = nil; tableView.delegate = nil; tableView.dataSource = nil; [tableView release]; tableView = nil; [super dealloc]; } -(void)viewDidLoad { ...... tableView.delegate = self; // **leak here** tableView.dataSource = self; // **leak here** DemoAppDelegate *appDelegate = (DemoAppDelegate *)[UIApplication sharedApplication].delegate; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleSaveNotification:) name:NSManagedObjectContextDidSaveNotification object:appDelegate.managedObjectContext]; </code></pre> <p>[self fetch];</p> <pre><code>} - (void)fetch { </code></pre> <p>NSError *error = nil; BOOL success = [self.fetchedResultsController performFetch:&amp;error]; if (!success) { debugLog(@"Unhandled error performing fetch: %@", [error localizedDescription]); NSAssert1(0, @"Unhandled error performing fetch: %@", [error localizedDescription]); } [tableView reloadData];</p> <pre><code>} - (NSFetchedResultsController *)fetchedResultsController { </code></pre> <p>if (fetchedResultsController == nil) { NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];</p> <p>DemoAppDelegate *appDelegate = (DemoAppDelegate *)[UIApplication sharedApplication].delegate; [fetchRequest setEntity:[NSEntityDescription entityForName:@"News" inManagedObjectContext:appDelegate.managedObjectContext]];</p> <p>fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:@"NewsCache"];</p> <pre><code>} </code></pre> <p>return fetchedResultsController; }</p> <pre><code> - (void)handleSaveNotification:(NSNotification *)aNotification { </code></pre> <p>DemoAppDelegate *appDelegate = (DemoAppDelegate *)[UIApplication sharedApplication].delegate; [appDelegate.managedObjectContext mergeChangesFromContextDidSaveNotification:aNotification]; [self fetch]; }</p> <pre><code> - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { News *news = [fetchedResultsController objectAtIndexPath:indexPath]; // fill cell.label.text according to the news field value } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload