2011-06-10 3 views
5

안녕하세요, UIWebView 기본 메모리 누수 문제가 있습니다. 다른 컨트롤러에서 UITableView에 링크가있는 WebView 표시 페이지가 있습니다. 탐색기와 WebView 함께 컨트롤러를 밀고 재산을 보유하십시오. 내가 좋아하는 인터넷에 모든 노력을 한IOS UIWebView 누설

는 :

-(void) viewWillAppear:(BOOL) animated 
{ 
    NSMutableURLRequest *_req=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:link] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:120]; 

    [_req setHTTPShouldHandleCookies:NO]; 
    [self setMyRequest:_req]; 
    [req release]; 
} 

[webView loadRequest:myRequest]; 

-(void) viewWillDisappear:(BOOL) Animated 
{ 
    [webView stopLoading]; 
    [webView loadHTMLString:@"<html></html>" baseURL:nil]; 
} 

- (void)dealloc { 
    [myRequest release]; 
    [webView stopLoading]; 
    [webView release]; 
    [link release]; 
    [super dealloc]; 
} 
:

[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"]; 

//Clear cache of UIWebView 
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; 
[NSURLCache setSharedURLCache:sharedCache]; 
[sharedCache release]; 
sharedCache = nil; 
[[NSURLCache sharedURLCache] removeAllCachedResponses]; 

이 내 코드입니다

이제는 시뮬레이터 4.2와 4.3에서만 테스트했습니다. xcode 4를 사용합니다. 네비게이터에서 뒤로 버튼을 누르면이 누출이 발생합니다. http://postimage.org/image/368r0g0xw/

어떤 도움이 난 지금, 감사

+0

코드 줄 중 하나가 다른 방법으로 표시되는 경우 실제 코드를 게시 할 수 있습니까? 그건 컴파일되지 않습니다. myRequest가 할당/할당되는 곳은 어디입니까? 'link'는 어떨까요? 'UIWebView'에 대한 델리게이트를 설정 했습니까? – Jim

+0

죄송합니다. 지금 코드를 게시하겠습니다. –

답변

1
-(void) viewWillAppear:(BOOL) Animated 
{ 
    //CONNECTION INIT 
    [web setDelegate:self]; 

    NSURL *url=[[NSURL alloc] initWithString:link]; 

    NSURLRequest *_req=[[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120]; 

    [self setReq:_req]; 

    [_req release]; 
    [url release]; 
    [web loadRequest:req]; 


} 

-(void) viewWillDisappear:(BOOL) Animated 
{ 
    [self setReq:nil]; 
    if ([web isLoading]) 
    [web stopLoading]; 
    [web setDelegate:nil]; 
} 

- (void)dealloc { 
    [req release]; 
    [map release]; 
    [web setDelegate:nil]; 
    [web release]; 
    [link release]; 
    [super dealloc]; 
} 

을 감상 할 수있다 :

그리고 여기가 화면에 링크 내있는 tableview 컨트롤러 여기

- (void)viewDidLoad { 
    webViewController=[[ItemDetail alloc] initWithNibName:@"ItemDetail" bundle:[NSBundle mainBundle] ]; 

    [super viewDidLoad]; 
} 


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

    webViewController.link=http://www.myLink.com; 
    [self.navigationController pushViewController:webViewController animated:YES]; 

} 

-(void) dealloc 
{ 
    [webViewController release]; 
    ... 
    ... 
    [super dealloc]; 
} 

의 코드입니다 내가 그것을 풀어 놓은 후에도 테이블 뷰로 돌아 간다면 여전히 3 MB 정도 남았지 만 지금은 다시 뷰를 생성하고 그것을 릴리스합니다. 그 3 메가 바이트에서 변경되지 않습니다. 이상한 ...

Item Detail *webViewController=[[ItemDetail alloc] initWithNibName:@"ItemDetail" bundle:[NSBundle mainBundle] ]; 


    [self.navigationController pushViewController:webViewController animated:YES]; 
    [webViewController release];