self.tableView.delegate = self and self.table.dataSource = self memory leak
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
It is astronomically unlikely that setting the delegate or dataSource property of a UITableView instance could cause a noticeable memory leak.
You should examine your surrounding code more thoroughly.
链接地址: http://www.djcxy.com/p/36104.html上一篇: 目标C:属性错误
下一篇: self.tableView.delegate = self和self.table.dataSource =自我内存泄漏