views:

997

answers:

2

Looking at the UIWebView docs, it says that

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

I'm creating a webview on the fly on a UITableView cell tap. I'm not sure how to do the "set delegate to nil" bit, since I don't track the UIWebView after its pushed to the navigation stack. I would have though that the UIWebView's dealloc method would be calling release on the delegate anyway?

Here's what I'm doing:

UIWebView *webView = [[UIWebView alloc] init];
UIViewController *viewController = [[UIViewController alloc] init];
// Start the webview loading, etc
...
// set the delegate
[webView setDelegate:self];
// Add the webview to the viewcontroller
[viewController setView:webView];
[[self navigationController] pushViewController:viewController animated:YES];
[webView release];
[viewController release];

I get the impression that I should write

[webView release];
[viewController release];
[self release]; // Releasing the delegate as per the docs.

But that seems kind of awkward, or have I interpreted it correctly?

+2  A: 

No. webView doesn't release/retain it's delegate it only assign it

UIWebView.h :

@property(nonatomic,assign) id<UIWebViewDelegate> delegate;

You can check [self retainCount] before and after [webView setDelegate:self] It'll be the same

oxigen
It still may be important to nil out the delegate pointer. I haven't seen it with the UIWebview, but MKMapView will sometimes keep sending delegate messages after it's been released (not sure why, seems to fall under unexpected behavior to me).
pzearfoss
So that means the docs are probably misleading? What I've written first is correct?
Paxton
A: 

Just spotted this, old now but maybe an answer will be useful to someone.

The docs are correct, you should set the delegate reference to nil before releasing the webview.

// set the delegate
[webView setDelegate:self];
// Add the webview to the viewcontroller
[viewController setView:webView];
[[self navigationController] pushViewController:viewController animated:YES];
**[webView setDelegate:nil];**
[webView release];
[viewController release];

or

webView.delegate = nil;

HTH

theLastNightTrain