2011-03-29 7 views
1

여기에 게시하기 전에 최선을 다했습니다. 아래는 메모리 누수의 원인이되는 코드입니다. autorelease를 포함하면 메모리 누출 문제를 해결할 수 있지만 여기에서 내가 뭘 잘못하고 있는지 알고 싶어합니다. 내가 소유하고 있다면, 나는 그것을 풀어 줘야한다. 내가하려고하는 것은 다음과 같다. 미리 도움을 청한다. ContainerView는 UIView이며 환영 텍스트를 추가하고 있습니다.UIImageView를 릴리스하고 여전히 메모리 누수가 발생했습니다.

UIImageView *welcomeText = [[UIImageView alloc] init]; 
welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero]; 
welcomeText.frame = (CGRect) { CGRectGetMidX(containerView.frame) -295 , 100.0, 590,134 }; 
welcomeText.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth; 
welcomeText.image = [UIImage imageNamed:@"welcome-to-the-jungle.png"]; 
[containerView addSubview:welcomeText]; 
[welcomeText release]; 
+0

"autorelease를 포함하면 메모리 누수가 수정됩니다."라는 말은 이미지보기를 자동 실행하고 해당 릴리스를 호출한다는 의미입니까? (BTW, 우스운 그림 이름) – Merky

답변

5
 
UIImageView *welcomeText = [[UIImageView alloc] init]; 
welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero]; 

이전에 welcomeText alloced 2 라인이 손실되고 해당 메모리가 누수 후. 첫 번째 줄 다음에는 이미지보기가 할당되고 welcomeText가이를 가리키고 있습니다. 두 번째 줄에서는 다른 이미지 뷰가 할당되고 이제는 welcomeText가이 새로운 뷰를 가리 킵니다. 따라서 첫 번째 할당 된 이미지 뷰에 대한 포인터가없고 누수 된 결과가됩니다.

여기서는 실제로 첫 번째 줄이 필요하지 않습니다.

UIImageView *welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero];
+0

+1 예. 나는 또한 동의했다. –

+0

감사합니다 :) 바보 같은 실수 – Nash

0

첫 번째 줄에는 UIImageView 개체를 할당하면 안됩니다. 두 번째 줄에는 첫 번째 줄에 할당 된 개체가 누수됩니다.

관련 문제