2011-08-25 5 views
1

나는 좋은 시간 동안 내 아이폰 애플 리케이션에서 메모리 누수 문제에 붙어되었습니다. 내 데이터를 잘못 읽어야하는 것 같아. 메모리를 할당 할 때마다 너무 많은 오버 헤드로 인해 누출이 발생할 수 있습니다. 데이터를 릴리스 할 때 메모리 사용량이 거의 떨어지거나 전혀 떨어지지 않는 것 같습니다. 하나는 2 일 낭비했습니다 내 flipside보기 컨트롤러에서 UIWebview가 URL을로드하고 내 앱의 메모리 사용량이 3MB에서 7로 점프합니다. 내 dealloc 메서드에서 webview를 릴리스하지만 메모리의 거대한 블록은 아직 살아 있습니다. 아무도 제안이 없나요?아이폰 어리석은 메모리 누수

- (void)viewDidLoad { 
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)]; 
[self.view addSubview:nav_bar]; 
[UINavigationBar release]; 

rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)]; 
item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"]; 
item.rightBarButtonItem = rightButton; 
item.hidesBackButton = YES; 
[nav_bar pushNavigationItem:item animated:NO]; 
[rightButton release]; 
[item release]; 

NSAutoreleasePool *initPool = [[NSAutoreleasePool alloc] init]; 

web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)]; 

web_view.autoresizesSubviews = YES; 
web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 

NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg"; 
NSURL *url = [NSURL URLWithString:urlAddress]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

[web_view loadRequest:requestObj]; 
[self.view addSubview:web_view]; 
[web_view release]; 
[initPool release]; 

[super viewDidLoad]; 
} 

- (void)dealloc { 
    [nav_bar removeFromSuperview]; 
    [web_view removeFromSuperview]; 
    [rightButton release]; 
    [super dealloc]; 
} 

들여 쓰기에 대해 사과드립니다. 지금 당황스럽고 처리하고 싶지 않습니다.

+0

Btw은 내가 시뮬레이터와 오토 릴리즈 풀을 내가 게시 할 때 내가 그 위에 보았다해야합니다 코드의 오래 된 조각에서 남은된다 – Daniel

답변

1

[UINavigationBar release]; -이게 뭐야? [nav_bar release]가 될 예정입니까? - 그렇다면 나중에 nav_bar에 다시 액세스하는 코드를 약간 더 아래로 수행해야합니다. 하지만 멤버 변수 인 것 같습니다. 그래서 그것은 dealloc에서 해제되어야합니다.

rightButton이 두 번 릴리스됩니다. 한 번은 viewDidLoad에, 한 번은 dealloc에 ​​있습니다.

자동 해제 풀이 무엇인지 설명해주세요.

+0

실제 장치 모두에서 이러한 누수를보고 있어요 – Daniel

2

참조 계산 방식에 대해 혼란스러워합니다. 아래

참조 의견 :

- (void)viewDidLoad { 
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

    UINavigationBar* nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)]; 
    // nav_bar retaincount 1 
    [self.view addSubview:nav_bar]; 
    // nav_bar retaincount 2 
    [nav_bar release]; 
    // nav_bar retaincount 1 - now controlled by self.view 

    UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)]; 
    // rightButton retaincount 1 

    UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"]; 
    // item retaincount 1 

    item.rightBarButtonItem = rightButton; 
    // rightButton retaincount 2 

    [rightButton release]; 
    // rightButton retaincount 1 - now controlled by item 

    item.hidesBackButton = YES; 
    [nav_bar pushNavigationItem:item animated:NO]; 
    // item retaincount 2 

    [item release]; 
    // item retaincount 1 - now controlled by nav_bar 

    UIWebView* web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)]; 
    // web_view retaincount 1 

    web_view.autoresizesSubviews = YES; 
    web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 

    NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg"; 

    NSURL *url = [NSURL URLWithString:urlAddress]; 
    // url is autoreleased, you can ignore.. 

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    // requestObj is autoreleased, you can ignore.. 

    [web_view loadRequest:requestObj]; 

    [self.view addSubview:web_view]; 
    // web_view retaincount 2 

    [web_view release]; 
    // web_view retaincount 1 - now controlled by self.view 

    [super viewDidLoad]; 
} 

- (void)dealloc { 
    // don't need to do anything because all the memory is controlled by self.view, will be released when the internal variable self.view is released. 
    [super dealloc]; 
} 
+0

필자가보기 엔 분명히 정리해 줘서 고맙지 만, 필자 사이드 뷰를 닫은 후에도 webview에 의해로드 된 데이터는 여전히 존재합니다. URL을 다시로드 할 필요가 없기 때문에 좋은데, 더 이상 캐시에 저장하지 않기를 바랍니다. 거기에 데이터를 해제 할 수있는 방법은 웹뷰에 의해 할당 취소 될 것이라고 생각하지만, 내 편리한 할당 그래프에 따르면되지 않습니다. – Daniel

+0

보유 수를 델타로 생각하는 것이 좋습니다. 보유 수를 더하거나 뺍니다. 위의 코멘트는 틀린 것 같습니다. 보유 수는 2, 5 일 수도 있고, 492 일 수도 있습니다. 중요한 것은 * 중요하지 않습니다 *. 당신은 당신이 일으킨 유지에 대해서만 걱정해야합니다. – bbum

+0

수는 아무 것도 될 수는 없지만 카운트가 바뀌고 대략적으로 양도 차가 발생하는 곳을 대략적으로 안내하는 것이 더 의미가 있습니다. –