我的很多內存泄露來自識別滑動的代碼。我究竟做錯了什麼?第一行是我認爲泄漏的東西(使用儀器)。它被示爲很多的錯誤負責主叫 這是在viewDidLoad中:使用手勢識別器進行內存管理
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
[(UISwipeGestureRecognizer *)swipeRight setNumberOfTouchesRequired:2];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self;
[webView addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
[(UISwipeGestureRecognizer *)swipeLeft setNumberOfTouchesRequired:2];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self;
[webView addGestureRecognizer:swipeLeft];
// Do any additional setup after loading the view from its nib.
}
還有一個問題,是什麼在這裏可以導致一個殭屍?我應該自動回收?
AViewController *a = [[AViewController alloc]init];
[self.navigationController pushViewController:a animated:YES];
a.title [email protected]"A View";
[a release];
更新3:我跑的儀器尋找壞的分配,並與一些集約利用我這裏得到殭屍: 錯誤消息:An Objective-C message was sent to a deallocated object (zombie) at address: 0xf583270.
在儀表這裏是我所看到的。儀器突出顯示這條線,並在其旁邊有100%。
AViewController *a = [[AViewController alloc]init];
很好的解釋!我仍然習慣於內存管理,這很有幫助! – Sum
順便說一句,我添加了一個額外的代碼的問題。那會是我想要使用autorelease的情況嗎?如果我們快速地來回切換,這不是一個問題嗎? – Sum
您添加的代碼是正確的。添加一個autorelease並刪除最終版本將導致相同的代碼。這不會是我上面提到的問題,因爲您不會在相同範圍內更改對象。我上面描述的問題非常少見,每當我提到autorelease功能時,我都會提及它。 – ColdLogic