I have a view containing a UIWebView which is loading a google map (so lots of javascript etc). The problem I have is that if the user hits the 'back' button on the nav bar before the web view has finished loading, it is not clear to me how to tidily tell the web view to stop loading and then release it, without getting messages sent to the deallocated instance. I'm also not sure that a web view likes its container view disappearing before it's done (but I've no choice if the user hits the back button before it's loaded).

In my viewWillDisappear handler I have this

map.delegate=nil;[self.map stopLoading];

this seems to handle most cases OK, as nil'ing the delegate stops it sending the didFailLoadWithError to my view controller. However if I release the web view in my view's dealloc method, sometimes (intermittently) I will still get a message sent to the deallocated instance, which seems to be related to the javascript running in the actual page, e.g.:

-[UIWebView webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:]: message sent to deallocated instance 0x4469ee0

If I simply don't release the webview, then I don't get these messages though I guess I'm then leaking the webview.

If I don't send the 'stopLoading' message, and simply release the webview within viewWillDisappear, then I see messages like this:

/SourceCache/WebCore/WebCore-351.9.42/wak/WKWindow.c:250WKWindowIsSuspendedWindow:  NULL window.

Possibly related, I sometimes (again totally intermittent) get an ugly heisenbug where clicking the back button on some other view's navbar will pop the title, but not the view. In other words I get left with the title of view n on the stack, but the view showing is still view n+1 (the result is you're trapped on this screen and cannot get back to the root view - you can go the other direction, i.e. push more views and pop back to the view that didn't pop corrrectly, just not to the root view. The only way out is to quit the app). At other times the same sequence of pushes and pops on the same views works fine.

This particular one is driving me nuts. I think it may be related to the view disappearing before the web view is loaded, i.e. in this case I suspect it may scribble on memory and confuse the view stack. Or, this could be completely unrelated and a bug somewhere else (i've never been able to reproduce it in debug build mode, it only happens with release build settings when I can't watch it with gdb :-). From my debug runs, I don't think I'm over-releasing anything. And I only seem to be able to trigger it if at some point I have hit the view that has the web view, and it doesn't happen immediately after that.

asked Apr 26 '09 at 0:15
frankodwyer
8,69453261
 
3  
   
If I were you, I'd file a bug with Apple. The web view should not be receiving any messages after it gets deallocated -- it ought to terminate JS processing and URL loading when it's dealloc'ed. I've seen the nav item issue occasionally, but only after I installed iPhone OS 3.0 beta. Maybe that's an OS bug, too? – Daniel Dickison May 5 '09 at 19:49
   
   
yes that's what I thought (dealloc should clean up). The apple docs aren't generally very clear on the contract with the objects they provide. I've never seen the nav thing before and it happens regularly enough in my program that I think it must be something I'm doing - hopefully related to this webview thing if I manage to fix it. – frankodwyer May 8 '09 at 12:28
   
   
oh and to add, this happens on 2.2.1 – frankodwyer May 8 '09 at 12:29
   
   
try setting the webView as associated object instead of ivar/property – myell0w Aug 4 '11 at 0:30

add comment (requires 50 reputation)

6 Answers

up vote41down voteaccepted

+125

A variation on this should fix both the leaking and zombie issues:

-(void)loadRequest:(NSURLRequest*)request
{[self retain];if([webView isLoading])[webView stopLoading];[webView loadRequest:request];[self release];}-(void)webViewDidStartLoad:(UIWebView*)webView
{[self retain];}-(void)webViewDidFinishLoad:(UIWebView*)webView
{[self release];}-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error
{[self release];}-(void)viewWillDisappear
{if([webView isLoading])[webView stopLoading];}-(void)dealloc
{[webView setDelegate:nil];[webView release];[super dealloc];}
answered May 5 '09 at 23:09
rpetrich
23.2k44171
 
   
   
yes good answer, i was thinking of trying something like this. your answer has a few things i didn't think of though, so i'll try it and if it works I will accept this one. Unfortunately the bounty will expire before i get time to try this - but what I will do is reopen the bounty and accept the answer in that case (just to remind myself, it was 250 bounty). – frankodwyer May 8 '09 at 12:24
   
   
I wouldn't worry about the bounty too much. It's just a number (besides, half is awarded to the top answer anyway ;) – rpetrich May 8 '09 at 23:54
   
   
finally got around to trying this - looking pretty good so far. No funny messages and it even seems to have cured the heisenbug - it was pretty intermittent though so I won't cheer yet til I have tested some more. – frankodwyer Jun 12 '09 at 21:03
   
   
@rpetrich: I know it's past 2nd anniversary of your answer, but why the fix is using retain/release onself? Wouldn't that cause problems for didReceiveMemoryNotification or forced call ofviewDidUnload? Could you please put some more light on why retain/release is used for a view controller which (I assume) has UIWebView outlet as property? – matm Jun 8 '11 at 9:12
   
   
@rpetrich: I think calling [self release] for [webView isLoading] == YES in viewDidUnloadshould balance retain count if e.g. webView has a long timeout, connection is slow and view is forced to unload. Am I correct? – matm Jun 8 '11 at 9:19

show 6 more comments

The UINavigationController bug you're describing in the second part of your post might be related to your handling of memory warnings. I've experienced this phenomenon and I"ve been able to reproduce it on view n in the stack by simulating a memory warning while viewing view (n+1) in the stack.

UIWebView is a memory eater, so getting memory warnings wouldn't be surprising when it's used as part of a view hierarchy.

answered Jun 30 '09 at 16:29
 
Armaggi
 
   
   
That's interesting, thanks. I will read up on what I'm meant to do in response to a memory warning (right now I don't handle it at all). I have not actually seen this issue since I moved to 3.0 and dumped the webview in favor of mapkit, and generally tightened up on other areas of memory handling. – frankodwyerJun 30 '09 at 16:50

add comment (requires 50 reputation)

A simple release message in dealloc ought to be enough.

Your second problem sounds like a prematurely deallocated view, but I can't say much without seeing some code.

answered Apr 26 '09 at 2:35
Can Berk Güder
32.8k1176109
 
   
   
yes a simple release in dealloc is what I had, and this is what results in the message getting sent to the deallocated instance. The webview is declared with @property (nonatomic,retain) if that makes a difference. Also, I have NSZombie checking on and otherwise I don't see any messages going to deallocated views - also as I say I can't reproduce this at all in Debug mode, it only happens in release mode. – frankodwyerApr 26 '09 at 9:36

add comment (requires 50 reputation)

There's a few ways to handle it, but this should work. You want the didFailLoadWithError message, it's what tells you it's stopped.

Set a flag isLeaving=YES; Send the Webview a stopLoading.

In didFailLoadWithError:, check for the error you get when the webview stops:

if ((thiserror.code == NSURLErrorCancelled) && (isLeaving==YES)) {

[otherClass performSelector:@selector(shootWebview) withObject:nil withDelay:0]

}

release the webView in shootWebview:


variations: if you want to be cavalier about it, you can do the performSelector:withObject:withDelay: with a delay of [fillintheblank], call it 10-30 seconds without the check and you'll almost certainly get away with it, though I don't recommend it.

You can have the didFailLoadWithError set a flag and clean it up somewhere else.

or my favorite, maybe you don't need to dealloc it all when you leave. Won't you ever display that view container again? why not keep it around reuse it?

Your debug being different from release issue, you might want to check your configuration to make sure that it's exactly the same. Bounty was on the reproducible part of the question, right? ;-).

-- Oh wait a second, you might be taking a whole View container down with the WebView. You can do a variation on the above and wait to release the whole container in shootWebView.

answered May 3 '09 at 7:47
 
   
   
my problem is that once I get the viewWillDisappear message, my view controller is going away and will shortly be dealloced (along with everything it contains, i.e. the webview). I don't see any way to prevent the view controller being dealloced (without leaking it) as it's not me that does that. Hence any delegate messages will (sometimes) wind up going to the dealloced instance of my controller. – frankodwyer May 3 '09 at 11:54
   
   
This sounds wonky even as I type it, but I suppose you could [self retain] before your webview loading calls and balance by [self release] in the didfinishload and didfailloads to make sure everything sticks around until the WebViews complete properly. – dieselmcfadden May 3 '09 at 23:49
   
   
yes I was thinking along those lines myself, and like you I thought it was a bit wonky - don't see why it shouldn't work tho. Will try this, along the lines of rpetrich's answer. – frankodwyer May 8 '09 at 12:23

add comment (requires 50 reputation)

Possibly related, I sometimes (again totally intermittent) get an ugly heisenbug where clicking the back button on some other view's navbar will pop the title, but not the view. In other words I get left with the title of view n on the stack, but the view showing is still view n+1 (the result is you're trapped on this screen and cannot get back to the root view - you can go the other direction, i.e. push more views and pop back to the view that didn't pop corrrectly, just not to the root view. The only way out is to quit the app). At other times the same sequence of pushes and pops on the same views works fine.

I have the same problem, when I'm use navigation controller with view controllers in stack > 2 and current view controller index > 2, if an memoryWarning occurs in this momens, it raises the same problems.

There is inly 1 solution, which I found after many experiments with overriding pop and push methods in NavigationController, with the stack of view controllers, with views and superviews for stacked ViewControllers, etc.

#import <UIKit/UIKit.h>#import <Foundation/Foundation.h>@interfaceFixedNavigationController:UINavigationController<UINavigationControllerDelegate>{}@end

#import "FixedNavigationController.h"static BOOL bugDetected = NO;@implementationFixedNavigationController-(void)viewDidLoad{[self setDelegate:self];}-(void)didReceiveMemoryWarning{// FIX navigationController & memory warning bugif([self.viewControllers count]>2)
bugDetected = YES;}-(void)navigationController:(UINavigationController*)navigationController
didShowViewController:(UIViewController*)viewController
animated:(BOOL)animated
{// FIX navigationController & memory warning bugif(bugDetected){
bugDetected = NO;if(viewController ==[self.viewControllers objectAtIndex:1]){[self popToRootViewControllerAnimated:NO];
self.viewControllers =[self.viewControllers arrayByAddingObject:viewController];}}}@end

It works fine for 3 view controllers in stack.

answered Sep 8 '09 at 7:33
abuharsky
358414
  add comment (requires 50 reputation)

I had a similar problem to this using a UIWebView in OS3 - this description was a good starting point, however I found than simply nil'ing out the web view delegate before releasing the webView solved my problem.

Reading the sample code (the accepted answer - above) - it seems like a lot of overkill. E.g. [webView release] and webView = nil lines do exactly the same thing given the way the author describes the variable is declared (so you don't need both). I'm also not fully convinced by all the retain and release lines either - but I guess your mileage will vary.

answered Aug 17 '09 at 14:34
TimM
1931211
  add comment (requires 50 reputation)

How to safely shut down a loading UIWebView in viewWillDisappear?的更多相关文章

  1. 简单的使用ehcache

    之前一直感觉缓存是高上大的东西,没有心思去研究.做了之后发现,简单的使用还是很容易的.这里记录ehcache在jfinal中的简单使用. 1.ehcahe简介 EhCache 是一个纯Java的进程内 ...

  2. leveldb

    [LevelDB] LevelDB is a fast key-value storage library that provides an ordered mapping from string k ...

  3. ehcache历史变迁及常用API的使用(转)

    ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案.同时ehcache作为开放源 ...

  4. iOS网络3—UIWebView与WKWebView使用详解

    一.整体介绍 UIWebView自iOS2就有,WKWebView从iOS8才有,毫无疑问WKWebView将逐步取代笨重的UIWebView.通过简单的测试即可发现UIWebView占用过多内存,且 ...

  5. IOS UIWebView 下拉刷新功能的简单实现

    1.运行效果图 2.swift 代码的实现 import UIKit class RefreshWebViewController: UIViewController,UIScrollViewDele ...

  6. 网络天荒地老之UIWebView&WebKit

    UIWebView 是苹果提供的用来展示网页的UI控件,它也是最占内存的控件. iOS8.0之后出现了webkit框架,WKWebView相比UIWebView节省了1/4~1/3的内存,速度快,但是 ...

  7. UIWebView的使用

    iOS中UIWebView的使用详解 一.初始化与三种加载方式 UIWebView继承与UIView,因此,其初始化方法和一般的view一样,通过alloc和init进行初始化,其加载数据的方式有三种 ...

  8. How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class

    This post will cover the basic setup and creation of an application with web content for iPhone that ...

  9. [iOS Hybrid实践:UIWebView中Html中用JS调用OC方法,OC执行JS代码]

    原理: 1.JS调用OC 每次webview执行跳转时都会被iOS给拦截,执行下面函数获得系统允许. 因此可以根据跳转信息转给系统,执行相应功能,比如打开相册等. // 网页中的每一个请求都会被触发 ...

随机推荐

  1. 基于jwt和角色的访问控制解决方案

    0,主要解决两个问题:1身份验证(防止httpclient拼接请求),2权限控制 1,身份验证使用jwt,在java就是jjwt jwt可以比较好的整合restful,对无状态客户端比较友好,(用se ...

  2. bzoj 3456 城市规划——分治FFT / 多项式求逆 / 多项式求ln

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3456 分治FFT: 设 dp[ i ] 表示 i 个点时连通的方案数. 考虑算补集:连通的方 ...

  3. Eclipse里git提交冲突rejected – non-fast-forward

    Eclipse里commit代码,其实只是提交到本地仓库,需要push才会提交到远程的git仓库,这时是一个本地仓库到远程仓库的同步过程.Git是分布式的,每个人在本地仓库维护本地的自己的那一份代码, ...

  4. Jvm垃圾回收器详细

    1, 串行回收器 1.1, 新生代串行回收器 (1)特点:  –它仅仅使用单线程进行垃圾回收  –它是独占式的垃圾回收  –进行垃圾回收时, Java应用程序中的线程都需要暂停(Stop-The-Wo ...

  5. file_put_contents(): supplied resource is not a valid stream resource

    在项目开发的过程中 自己想把输出和一些想要内容输出到日志文件中,便于查看 但是在输入的过程中报了这样一个错误: file_put_contents(): supplied resource is no ...

  6. xml处理模块

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...

  7. c#编写的基于TCP通信的微风IM 版本3 新年新UI

    电商商模 背景:来源,产生运营模式:模式特点,服务对象,业务开展,赢利点,扩 张点,定价策略行业分析:市场分析:DX,企业,政策,经济,文化,技术 网站架构:频道,版块,功能体系 项目推广:地面推广, ...

  8. mul 指令

    mul 是乘法指令 两个相乘的数:两个相乘的数,要么都是 8 位,要么都是 16 位. 如果是 8 位,一个默认放在 AL 中,另一个放在 8 位寄存器或内存字节单元中: 如果是 16 位,一个默认在 ...

  9. js的console你知道多少

    js的console你知道多少? 列出所有的console属性 console.dir(console) 或者 console.dirxml(console) 记录代码执行时间 console.tim ...

  10. Strom简单介绍

    1.离线计算是什么? 离线计算:批量获取数据.批量传输数据.周期性批量计算数据.数据展示 代表技术:Sqoop批量导入数据.HDFS批量存储数据.MapReduce批量计算数据.Hive批量计算数据. ...