2011-07-15 32 views
0

我的很多內存泄露來自識別滑動的代碼。我究竟做錯了什麼?第一行是我認爲泄漏的東西(使用儀器)。它被示爲很多的錯誤負責主叫 這是在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]; 

回答

0

內存管理的東西,需要多一點的時間來太肯定的。我親自讓操作手柄爲我處理所有事情。這意味着每次我分配一些東西,我只是給它一個autorelease。操作系統將在需要時爲我處理該版本。唯一的問題是當你在同一個作用域中重用一個對象時,操作系統會發送太多的發佈並釋放內存,然後再使用它。下面是一個例子

//This code will result in a memory crash 
CustomObject *coolThing = [[[CustomObject alloc] init] autorelease]; 
[coolThing setAwesomeLevel:10]; 
[array addObject:coolThing]; 

[coolThing setAwesomeLevel:7]; 
[array2 addObject:coolThing]; 

相反,你會使用

//Working code 
CustomObject *coolThing = [[CustomObject alloc] init]; 
[coolThing setAwesomeLevel:10]; 
[array addObject:coolThing]; 

[coolThing setAwesomeLevel:7]; 
[array2 addObject:coolThing]; 

[coolThing release]; 

現在,使用自動釋放你的代碼,你會做的是將它們添加到allocs。這就是爲什麼你的代碼泄漏。當您將其添加到webView對象時,它將增加其保留帳戶。當你離開這個範圍時,它有一個2的保留帳戶,但你只發送一次發佈(其保留將保持爲1,並且從不釋放內存)。

UISwipeGestureRecognizer *swipeRight = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)] autorelease]; 
[(UISwipeGestureRecognizer *)swipeRight setNumberOfTouchesRequired:2]; 

swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
swipeRight.delegate = self; 
[webView addGestureRecognizer:swipeRight]; 

UISwipeGestureRecognizer *swipeLeft = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)] autorelease]; 
[(UISwipeGestureRecognizer *)swipeLeft setNumberOfTouchesRequired:2]; 
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
swipeLeft.delegate = self; 
[webView addGestureRecognizer:swipeLeft]; 

如果您不想像這樣使用autorelease,則需要在將手勢添加到webView後簡單添加一些版本。

[swipeRight release]; 
[swipeLeft release] 
+0

很好的解釋!我仍然習慣於內存管理,這很有幫助! – Sum

+0

順便說一句,我添加了一個額外的代碼的問題。那會是我想要使用autorelease的情況嗎?如果我們快速地來回切換,這不是一個問題嗎? – Sum

+1

您添加的代碼是正確的。添加一個autorelease並刪除最終版本將導致相同的代碼。這不會是我上面提到的問題,因爲您不會在相同範圍內更改對象。我上面描述的問題非常少見,每當我提到autorelease功能時,我都會提及它。 – ColdLogic

4

你是分配/初始化UISwipeGestureRecognizer(這使得你的工作,釋放),而不是釋放它在你的上面一段代碼,兩次。在將這些添加到您的webview後,您需要添加[swipeRight release];[swipeLeft release];

+0

明白了。我正在測試它! – Sum

+0

似乎已經解決了大部分手勢識別器泄漏。 :) – Sum

+0

但是,另外一個問題,我添加了一些問題(代碼不會在評論中格式化) – Sum

1

添加手勢大家的意見後,調用就可以了release方法,因爲手勢是保留意見在其中添加。

像下面對象的C

[webView addGestureRecognizer:swipeRight]; 
    [swipeRight release]; 

而且

[webView addGestureRecognizer:swipeLeft]; 
    [swipeLeft release]; 
+0

對,我認爲應該解決它。我不知道我怎麼了。我正在測試,現在:) – Sum