1

I am playing youtube videos on a UIWebView which appears as a modalViewController subview (flip transition). Everything works fine, even though the UIWebView is released, I still receive memory warnings after a few repeated selection of this modalViewController.

I have added my UIWebView programmatically inside ViewDidLoad. Inside viewDidDisappear I check for [UIWebView retainCount] and if greater than 1, perform the following steps:

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;

NSLog(@"[self.webView retainCount] %d", [self.webView retainCount]);

I am running my code on xCode 3.2.5, iOS 4.2.

Appreciate all you help.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
random
  • 10,238
  • 8
  • 57
  • 101

2 Answers2

5

I think you are approaching the memory management problem in the wrong way. Checking the retainCount is a valid debugging technique if you know what you are doing. It is not, however, a memory management tool. In your particular case, if the UIWebView is being displayed it will always have retain count > 1. The superview will have a retain on it thus making the "if" useless.

If the webView property is well defined (i.e. noatomic, retain) the statement:

 self.webView = nil;

should release the webView. A common mistake is to initialize the property with:

self.webView = [[UIWebView alloc] init];

This is likely to introduce a leak if the webView is defined as "retain". The correct way is

self.webView = [[[UIWebView alloc] init] autorelease];

If you can't display your controller several times without running out of memory you have a memory leak. Use Instruments (Leaks in particular) to find hte objects what are note being released properly. This is a good tutorial.

Be careful in keeping your retains and releases balanced and check for leaks.

fsaint
  • 8,759
  • 3
  • 36
  • 48
  • Thanks for your response. Actually, I did initialize webview as follows:self.webView = [[[UIWebView alloc]initWithFrame:CGRectZero] autorelease]; Through Instruments, I am unable to detect any leak on view dismissal. But the control reaches didReceiveMemoryWarning method with Level 1 warning and the app crashes. – random Dec 13 '10 at 10:00
  • @techenthusiast Have you found the solution It will be very useful if you post the solution!!! Same warning repeated and crashed in mycase am using four webviews loading images – Gobi M May 01 '13 at 12:27
0

Your problem will be related to this: Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?

Scroll down to my answer for an extension of the accepted answer - i had this problem for days and it's now resolved!

Community
  • 1
  • 1
PostCodeism
  • 1,070
  • 1
  • 12
  • 20