UIWebView不使用ARC释放所有实时字节 [英] UIWebView not freeing all live bytes using ARC

查看:70
本文介绍了UIWebView不使用ARC释放所有实时字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

  / * 

有几个关于UIWebView内存泄漏的理论和传言,以及
如何正确处理UIWebView实例的清理工作在解除分配之前。这个
方法实现了其中一些建议。

#1:各种开发人员认为UIWebView可能无法正常丢弃
对象&视图没有强制UIWebView在
dealloc之前加载空内容。

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

* /
[webView loadHTMLString:@基本URL:无];

/ *

#2:其他人声称如果在dealloc期间加载内容
,UIWebView将会泄漏。

来源:http://stackoverflow.com/questions/6124020/uiwebview-leaking

* /
[webView stopLoading];

/ *

#3:Apple建议在重新分配之前将委托设置为nil:
重要:在发布已设置$的UIWebView实例之前b $ ba委托,在
处理UIWebView实例之前,必须先将UIWebView委托属性设置为nil。这可以在例如处理UIWebView的
dealloc方法中完成。

来源:UIWebViewDelegate类引用

* /
[webView setDelegate:nil];


/ *

#4:如果您为任何给定视图创建多个子视图,并且您尝试取消分配一个
老子,父子
视图指向该子,并且在父视图消失之前不会实际释放。下面的这个
调用可确保您不会创建许多子视图,这些视图将挂起
,直到取消分配父视图。
* /

[webView removeFromSuperview];


I am currently building a navigation controller app in iOS 5.1 that uses ARC. I often need to display webpages and I have made a web viewer that is just a UIWebView with some custom content around the sides. When the user is finished looking at the page they hit the back button which should release all of the memory associated with the custom web viewer. My problem is that all of the memory does not appear to be released when the back button is hit. I have built a toy app (on github) that is just a couple of buttons each having a first responder that calls a different page.

@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];
}

The CustomWebView is just a UIWebView linked in IB to UIWebView Property

@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];
}

My problem is that I set the heap baseline to be the initial ViewController once everything is loaded. I then check the heap after loading a page then returning to the ViewController and get the following heapshots:

Which shows that after each series of clicking on a button and returning to the ViewController the heap continues to grow even though the CustomWebView should all be released.

EDIT:

The sample project described above can be found on github

解决方案

I had the same-ish problem, the i found this little piece of magic

/*

 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];

这篇关于UIWebView不使用ARC释放所有实时字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆