Hello everyone,
I have following codes, which have memory leak on device, could you please kindly help check it? Thanks.
@interface NewsListViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {
@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];
[self fetch];
}
- (void)fetch {
NSError *error = nil; BOOL success = [self.fetchedResultsController performFetch:&error]; if (!success) { debugLog(@"Unhandled error performing fetch: %@", [error localizedDescription]); NSAssert1(0, @"Unhandled error performing fetch: %@", [error localizedDescription]); } [tableView reloadData];
}
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) { NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
DemoAppDelegate *appDelegate = (DemoAppDelegate *)[UIApplication sharedApplication].delegate; [fetchRequest setEntity:[NSEntityDescription entityForName:@"News" inManagedObjectContext:appDelegate.managedObjectContext]];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:@"NewsCache"];
}
return fetchedResultsController; }
- (void)handleSaveNotification:(NSNotification *)aNotification {
DemoAppDelegate *appDelegate = (DemoAppDelegate *)[UIApplication sharedApplication].delegate; [appDelegate.managedObjectContext mergeChangesFromContextDidSaveNotification:aNotification]; [self fetch]; }
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
News *news = [fetchedResultsController objectAtIndexPath:indexPath];
// fill cell.label.text according to the news field value
}
@end