UIWebView不使用ARC释放所有实时字节

bre*_*dan 9 memory iphone uiwebview ios ios5

我目前正在iOS 5.1中构建一个使用ARC的导航控制器应用程序.我经常需要显示网页,我创建了一个Web查看器,它只是一个UIWebView,旁边有一些自定义内容.当用户完成查看页面时,他们点击后退按钮,后退按钮应释放与自定义Web查看器关联的所有内存.我的问题是,当按下后退按钮时,所有内存似乎都没有被释放.我已经构建了一个玩具应用程序(在github上),它只是几个按钮,每个按钮都有一个调用不同页面的第一响应者.

@implementation ViewController
-(IBAction)googlePressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.google.com"];
   [self.navigationController pushViewController:customWebView animated:NO];
 }
-(IBAction)ksbwPressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.ksbw.com/news/money/Yahoo-laying-off-2-000-workers-in-latest-purge/-/1850/10207484/-/oeyufvz/-/index.html"];
   [self.navigationController pushViewController:customWebView animated:NO];
}
-(IBAction)feedProxyPressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://feedproxy.google.com/~r/spaceheadlines/~3/kbL0jv9rbsg/15159-dallas-tornadoes-satellite-image.html"];
   [self.navigationController pushViewController:customWebView animated:NO];
}
-(IBAction)cnnPressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.cnn.com/2012/04/04/us/california-shooting/index.html?eref=rss_mostpopular"];
   [self.navigationController pushViewController:customWebView animated:NO];
}
Run Code Online (Sandbox Code Playgroud)

CustomWebView只是在IB到UIWebView属性中链接的UIWebView

@implementation CustomWebView

@synthesize webView, link;

- (id)initWithStringURL:(NSString *) stringURL
{
   if (self = [super init]) {
       link = stringURL;
   } 
   return self;
}


- (void)viewDidLoad
{
   [super viewDidLoad];
   NSURL *url = [NSURL URLWithString:link];
   NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
   [webView loadRequest:urlRequest];
}

- (void)viewDidUnload
{
   [super viewDidUnload];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,一旦加载了所有东西,我就将堆基线设置为初始的ViewController.然后我在加载页面后检查堆,然后返回到ViewController并获得以下快照:

Heapshots

这表明,在每次点击一个按钮并返回到ViewController之后,即使CustomWebView都应该被释放,堆也会继续增长.

编辑:

上面描述的示例项目可以在github上找到

Dan*_*son 8

我有同样的问题,我找到了这个小小的魔法

/*

 There are several theories and rumors about UIWebView memory leaks, and how
 to properly handle cleaning a UIWebView instance up before deallocation. This
 method implements several of those recommendations.

 #1: Various developers believe UIWebView may not properly throw away child
 objects & views without forcing the UIWebView to load empty content before
 dealloc.

 Source: http://stackoverflow.com/questions/648396/does-uiwebview-leak-memory

 */        
[webView loadHTMLString:@"" baseURL:nil];

/*

 #2: Others claim that UIWebView's will leak if they are loading content
 during dealloc.

 Source: http://stackoverflow.com/questions/6124020/uiwebview-leaking

 */
[webView stopLoading];

/*

 #3: Apple recommends setting the delegate to nil before deallocation:
 "Important: Before releasing an instance of UIWebView for which you have set
 a delegate, you must first set the UIWebView delegate property to nil before
 disposing of the UIWebView instance. This can be done, for example, in the
 dealloc method where you dispose of the UIWebView."

 Source: UIWebViewDelegate class reference    

 */
[webView setDelegate:nil];


/*

 #4: If you're creating multiple child views for any given view, and you're
 trying to deallocate an old child, that child is pointed to by the parent
 view, and won't actually deallocate until that parent view dissapears. This
 call below ensures that you are not creating many child views that will hang
 around until the parent view is deallocated.
 */

[webView removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)


Nit*_*bur 0

我还没有使用过 ARC,但是看看你的自定义 webview 代码,我认为你创建的 webview 仍然存在。

如果你想确保你的 webview 被正确杀死,请确保在 viewWill/DidDisappear 中,调用 [webview stopLoading],并将 webview 归零。

(注意:如果你推送另一个视图,那么 webview 也会被杀死,所以一定要处理好这些问题)

大多数情况下,我遇到了 webView 问题,即使弹出 viewController 后,回调某些 webView 委托方法也会使应用程序崩溃。(我猜 ARC 可以防止它崩溃)。

让我知道这是否有帮助。