Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting UIWebView delegate property to nil

According to documentation, UIWebView delegate property needs to be set to nil before disposing of the UIWebView instance.

My question is : If the UIWebView is setup in IB with its delegate points to the file owner which is a viewController. And the UIWebView and its superview is never disposed of during the course of execution, does the delegate need to be set to nil ?

Am asking this because it seems difficult to get a reference to UIWebView.

Hope somebody knowledgeable could give some hints.

(non-arc setup)

like image 419
Stanley Avatar asked Jan 28 '26 09:01

Stanley


1 Answers

It is always good practice to follow the suggestions in the documentation. In most cases, what you're suggesting is generally correct: there isn't really a need to set the delegate to nil as the only time the object will be destroyed is when execution ends. However, that is similar to saying "I don't need to free this heap space because it will only be freed immediately before main returns"...

A more pressing problem is what happens if the application receives a low memory warning. If a memory warning is received and the UIWebView is unloaded and the delegate is NOT set to nil, then you may end up with a memory leak behind the scenes depending on what UIWebView does when it sets up/removes its delegate.

If you set the delegate to nil on destruction, then whatever needs to happen is handled by UIWebView when it detects its delegate is set to nil. If you don't, and the instance is unpredictably destroyed (say, due to a low memory warning), then once the warning has cleared, the loading will take place again and whatever procedure is taken to set a UIWebView's delegate will be run twice, without the 'shut down' procedure ever happening in between. Given their insistence that you set the delegate to nil on destruction, I imagine this manifests itself as a memory leak or something similarly sinister.

like image 138
Ephemera Avatar answered Jan 29 '26 23:01

Ephemera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!