2011-12-26 3 views
2

didSelectRowAtIndexPath를 사용하여 tableView에서 viewController를 푸시하면 iOS 4.3에서 충돌이 발생하지만 iOS 5.0 이상에서는 정상적으로 작동하는지 파악하려고합니다. 내가 전화 할 때Object dealloc은 iOS 4.3에서만 충돌합니다.

그것은 바로 충돌 :

self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease]; 

언제 처음 후에 customViewController이 추진되고있다.

@property (nonatomic, retain) CustomViewController *customViewController; 

-(void) dealloc // Dealloc of tableView. 
{ 

[customViewController release]; 
customViewController = nil; 

} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease]; // Release old, allocate new, set it. 

[[self navigationController] pushViewController:customViewController animated:YES]; 
[customViewController release]; // Balance out pushViewController's retain. 


} 

감사 :

여기 내 관련 코드입니다.

답변

2
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease]; 
[[self navigationController] pushViewController:customViewController animated:YES]; 
[customViewController release]; // Balance out pushViewController's retain. ---->NO 
} 

마지막 release은 필요하지 않습니다 여분의 하나입니다.
에 이미 retain count이 하나만 있습니다.


우리는 그렇게 나중에 0이 될 것이다 실행의 (아마 끝을 수를 유지하기에 당신은 autorelease 말을 당신이 CustomViewController이 카운트 == 1.
을 유지 만들

self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease]; 

이 줄을 분석합니다 루프),하지만 지금은 여전히 ​​1 이니까 여전히 액세스 할 수 있지만 0으로 취급하십시오.
그 후 당신은 self.customViewController라고 말하면 그 속성은 유지되므로 카운트 == 1을 유지하십시오. 그리고 당신은 당신의 dealloc. 귀하의 코멘트로
:과 pushViewController의이 유지

// 잔액이 부족합니다.

Balance이 아닌 경우 YOU own 만 균형을 유지합니다. 시스템이 오브젝트에 대해 보유하면 시스템에서 더 이상 필요하지 않을 때 오브젝트를 해제합니다.

+0

심각하게 기다리시겠습니까? viewController를 유지한다고 생각했지만 실제로 뷰를 유지한다고 생각하십니까? –

+0

@JoeRaythein보기 컨트롤러를 유지하지만 해제합니다. 그것의 소유권이 필요합니다.autorelease를 호출했을 때 이미 소유권을 포기했습니다. –

+0

하지만'release'는 컨트롤러가 눌려 질 때 앱을 충돌시키지 않아야한다고 생각합니다.'didSelectRowAtIndexPath'가 아닌'dealloc' 메쏘드에서 오류를 일으킬 것입니다. – Ecarrion

1

customViewController를 해제하지 마십시오. 이미 과제를 할당 할 때 이미 자동 리트 리를 했으므로 이미 alloc에서 소유권을 포기했습니다. 객체를 다시 릴리스하거나 자동 릴리스하지 않아도됩니다. 내비게이션 컨트롤러는 소유권을 가지며 적절한 시간에 소유권을 양도합니다.

또한 우연히도 다른 버전이 아닌 한 버전에서 볼 수 있습니다. 이것은 메모리 관리 문제이므로 볼 때 나타날 수있는 손상 (충돌 등)은 앱을 실행할 때마다 장치의 메모리 상태에 따라 달라집니다. 항상 Skype를 실행하고 Photos 앱을 열기 전에 만 충돌이 발생합니다.

디버깅 세션 중에 좀비를 사용하는 것이 좋습니다. 좀비가 활성화되면 객체는 실제로 릴리스되지 않습니다. 대신 그들은 좀비 상태에 놓이게됩니다. 다시 메시지를 보내면 앱을 중단하고 메모리 문제를 디버그하는 데 도움이되도록 이탈 한 메시지가 어디서 왔는지 보여줍니다.

+0

매우 유익한, 고마워. 나는 내 방식의 오류를 본다! –

관련 문제