Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIWebView未使用ARC释放所有活动字节_Iphone_Ios_Memory_Ios5_Uiwebview - Fatal编程技术网

Iphone UIWebView未使用ARC释放所有活动字节

Iphone UIWebView未使用ARC释放所有活动字节,iphone,ios,memory,ios5,uiwebview,Iphone,Ios,Memory,Ios5,Uiwebview,我目前正在iOS 5.1中构建一个使用ARC的导航控制器应用程序。我经常需要显示网页,我制作了一个web查看器,它只是一个UIWebView,在其两侧有一些自定义内容。当用户完成查看页面时,他们点击后退按钮,该按钮将释放与自定义web查看器关联的所有内存。我的问题是,当按下后退按钮时,所有内存似乎都没有释放。我构建了一个玩具应用程序(),它只是几个按钮,每个按钮都有一个调用不同页面的第一响应程序 @implementation ViewController -(IBAction)googlePr

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

@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];
}
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];
}
我的问题是,一旦加载了所有内容,我就将堆基线设置为初始ViewController。然后,我在加载页面后检查堆,然后返回到ViewController,并获得以下堆照片:

这表明,在每次单击按钮并返回ViewController之后,堆继续增长,即使CustomWebView应该全部释放

编辑:


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

我还没有使用ARC,但是查看您的自定义webview代码,我认为您创建的webview仍然有效

如果要确保正确终止webview,请确保在viewWill/did中消失,调用[webview stopLoading],然后取消webview

(注意:如果您正在推送另一个视图,那么webview也将被关闭,因此请确保妥善处理这些问题)

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


如果有帮助,请告诉我。

Brendan几点:


您没有发布足够的代码来确定发生了什么。但是为什么不让控制器拥有一个UIWebView,而不是在每个按钮上创建另一个UIWebView,只需传递一个带有相关URL的字符串?这是一种更好的设计模式,可以保证您身边不会有超过一个的模式。

我使用ARC已经有一段时间了,我发现在过去,web视图将保留已加载的内容,直到它被告知加载其他内容。我解决这个问题的方法是在view will(或Did)dissapear方法中使用javascript调用,如下所示:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.webView loadHTMLString:@"<html></html>" baseURL:nil];
}
-(void)视图将消失:(BOOL)动画
{
[超级视图将消失:动画];
[self.webView loadHTMLString:@“baseURL:nil];
}

这为我释放了内存。

UIWebView是一个内存猪,不能很好地释放内存。我在CustomWebView类中放置了一个-(void)dealloc调用,当按下后退按钮时,它会按预期进行调用


您可以尝试[[NSURLCache sharedURLCache]removeAllCachedResponses]或使用,然后释放您自己的NSURLCache副本,但坦率地说,UIWebView从来不会在自身之后完全清理。

我发现,我也有同样的ish问题


@界面WebViewController:UIViewController

-(void)viewDidLoad
{
[超级视图下载];
NSString*urlAddress=@”http://www.google.com";
NSURL*url=[NSURL URLWithString:urlAddress];
NSURLRequest*requestObj=[nsurlRequestRequestWithURL:url];
self.webView.delegate=self;
[self.webView加载请求:requestObj];
}
-(无效)视图将消失:(BOOL)已设置动画{
[超级视图将消失:动画];
[_webView停止加载];
[_webviewloadhtmlstring:@”“baseURL:nil];
[_webView停止加载];
[_webView从SuperView移除];
}
-(无效)解除锁定
{
[_webView停止加载];
}
-(void)webView:(UIWebView*)webView失败加载错误:(NSError*)错误
{
[_webView停止加载];
_网络视图=零;
}

Calvin,谢谢您的回复,但是调用stopLoading和nil'ing webview不会改变堆显示或总活动字节数。对于这些密集型页面,只在Mobile Safari中打开会更好吗?上面演示的这个示例项目表明,很容易证明UIWebView在释放所有内容后仍会留下内存占用。WebView在资源消耗方面非常昂贵。我不知道为什么你的总活咬数没有变化。在ViewController iActions中,在导航堆栈上按下ViewController后,是否释放正在创建的对象?我之前错过了这个细节。理想情况下,它应该是-(iAction)googlePressed:(id)sender{CustomWebView*CustomWebView=[[CustomWebView alloc]initWithString:@];[self.navigationController pushViewController:CustomWebView动画:否];[CustomWebView发布];]我正在使用ARC,它禁止消息发送到发布。根据我对ARC的理解,它将被自动调用。我不确定我是如何错过了你问题中的ARC细节。现在我不确定,为什么它不发布。你确定僵尸被禁用了吗?在黑暗中拍摄,但尝试使你的IBOutlet属性(弱)。我只以指定的方式尝试了一个,但我也遇到了同样的问题。我在github上发布了一个示例项目,以显示我的内存问题。我快速查看了您在Git上的代码,没有发现任何明显的问题,但我想我必须在最终分析中说:问题是什么?您使用的是ARC。内存管理不是您的问题。除非您e看到泄漏,您发布的屏幕截图中没有指出,问题究竟出在哪里?尝试使用分配工具,查看实例化了哪些对象及其计数。我的问题是,发布在git上的代码显示,在从具有web视图的视图控制器返回时,我没有获取所有内存。如果应用程序e只是一个web查看器这可能没问题,但这只是应用程序的一小部分,随着时间的推移,我最终会收到内存不足的警告。我更愿意避免这些内存问题,但只需将应用程序的状态保存在viewDidUnload中就可以了,只要没有内存问题就不用担心了
/*

 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];
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.webView.delegate = self;
[self.webView loadRequest:requestObj];
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[_webView stopLoading];
[_webView loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];
[_webView stopLoading];
[_webView removeFromSuperview];
}

- (void)dealloc
{
[_webView stopLoading];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[_webView stopLoading];
_webView = nil;
}