我有以下代碼,儀器表明pushViewController方法在設備上有32個字節的內存泄漏。請你幫忙檢查一下我打破了什麼規則?我是否應該將某些「保留」更改爲「分配」以進行申報?提前致謝!pushViewController內存泄漏
@interface GuideNewsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
@private
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
UITableView *tableView;
NewsListViewController *newsListViewController;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain, readonly) NewsListViewController *newsListViewController;
@implementation GuideNewsViewController
......
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Member *member = [fetchedResultsController objectAtIndexPath:indexPath];
self.newsListViewController.managedObjectContext = self.managedObjectContext;
self.newsListViewController.title = member.memberName;
self.newsListViewController.author = member;
**// leak here**
[self.navigationController pushViewController:self.newsListViewController animated:YES];
}
......
@end
@interface NewsListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
@private
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
UITableView *tableView;
Member *author;
}
@property (nonatomic, assign) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign, readonly) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, assign) Member *author;
@end
我不認爲這是不正確的。因爲newsListViewController是一個成員,所以它應該從GuideNewsViewController的dealloc方法中釋放,如下所示:[newsListViewController release]; 但你有一點,沒有必要使它成爲會員,在這種情況下最好使用委託(如有必要) – nacho4d 2010-09-05 08:14:37
@ nacho4d,託羅,謝謝你的回覆。 self.newsListViewController將在GuideNewsViewController的dealloc方法中發佈,設置newsListViewController的目的爲GuideNewsViewController的屬性重新使用它。 因爲我是iphone的新開發者。您能否提供一些「使用委託通知自己」的示例代碼?謝謝。 – mobguang 2010-09-05 08:54:38
我會試試看。謝謝大家! – mobguang 2010-09-05 16:55:08