views:

46

answers:

1

Hi,

after multiple days of banging my head against the wall and having sleepless nights I'm hoping to find some help here. I've gone through various posts here, but none of the answers seem to provide a resolution for me.

In short, my problem is that my App crashes after heavy usage (>10min) of the UIWebView (e.g. opening larger news paper sites in series - one after the other).

To give more details: I am writing an iPhone App and from the MainViewController I push a browserViewController on the navigationController. The browserViewController loads a nib which contains a UWebView (I do not create the WebView programatically). The UIWebView is wired up using Interface Builder.

When going back to Main and then going again to the browserViewController, I only recreate the browserViewController if it is nil. (I want to keep the content that is loaded i the UIWebView - only if there is a memory warning shoud this view be unloaded and release all memory used).

In both, MainViewController and browserViewController I am responding to memory warnings, but this seems not to provide enough relief.

Looking at Instruments I noticed that for example CFData(store) keeps increasing. And even if I simulate a memory warning (see code below) and call viewDidUnload on browserViewController, CFData remains allocated and does not get freed.

So my biggest question is:
How to free up memory created from "browsing"?

This counts for two cases:
- how do I make sure that viewDidUnload properly frees memory allocated my CFData(store)?
- how to free up memory when the user keeps loading pages in browserViewController?
.
Who manages CFData?

See below for my simplified sample code:
.
.

MainViewController.h

//  MainViewController.h
#import "myAppDelegate.h"
#import "BrowserViewController.h"

@interface MainViewController : UIViewController {
 BrowserViewController *browViewController;
}

- (void) switchToBrowserViewController;

@end

.
.

MainViewController.m

//  MainViewController.m
#import "MainViewController.h"

@implementation MainViewController

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 [browViewController release];
 browViewController = nil;
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}

- (void)dealloc {
 [browViewController release];
 browViewController = nil;
 [super dealloc];
}

- (void) switchToBrowserViewController {

 // create new browViewController if needed
 if ( browViewController == nil ) {
  browViewController = [[BrowserViewController alloc]     initWithNibName:@"BrowserViewController" bundle:nil];
 }
 browViewController.navigationItem.hidesBackButton = YES;

 [((myAppDelegate *)[UIApplication sharedApplication].delegate).navController setNavigationBarHidden:YES animated:NO];

 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration: 1];
 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:
  ((myAppAppDelegate *)[UIApplication sharedApplication].delegate).navController.view cache:YES];

 [((myAppAppDelegate *)[UIApplication sharedApplication].delegate).navController pushViewController:browViewController animated:NO];
 [UIView commitAnimations];

}

@end

.
.
.
.

BrowserViewController.h

//  BrowserViewController.h

#import <UIKit/UIKit.h>
#import "myAppDelegate.h"

@interface BrowserViewController : UIViewController <UIWebViewDelegate> {
 IBOutlet UITextField *browserURLField;
 IBOutlet UIWebView *browserWebView;
}

@property (nonatomic, retain) UIWebView *browserWebView;

- (void) loadURLinBrowser;

@end

.
.

BrowserViewController.m

//  BrowserViewController.m
#import "BrowserViewController.h"

@implementation BrowserViewController
@synthesize browserWebView;

- (void)viewDidLoad {
 [browserWebView setDelegate:self];
 [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload { 
 [super viewDidUnload];

 [browserWebView stopLoading];
 [browserWebView setDelegate:nil];
 [browserWebView removeFromSuperview];
 [browserWebView release];
 browserWebView = nil;

 browserURLField = nil;
}

- (void)dealloc {
     [browserURLField release];

 browserWebView.delegate = nil;
 [browserWebView stopLoading];
 browserWebView = nil;
 [browserWebView release];

 [super dealloc];
}

- (void) switchBackToMainViewController {

 [((myAppDelegate *)[UIApplication sharedApplication].delegate).navController setNavigationBarHidden:NO animated:NO];

 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration: 1];
 [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:((myAppAppDelegate *)[UIApplication sharedApplication].delegate).navController.view cache:YES];

 [((myAppAppDelegate *)[UIApplication sharedApplication].delegate).navController popViewControllerAnimated:NO];

 [UIView commitAnimations];
}

- (void) loadURLinBrowser {
 NSURL *url = [[NSURL alloc] initWithString:browserURLField.text];
 NSMutableURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
 [browserWebView loadRequest: request];
 [request release];
 [url release];
}

@end

.
.
.
I have tried various recommendations from other posts. For example:
1) Loading an empty page into the WebView.

NSString *html = @"<html><head></head><body></body></html>";
[browserWebView loadHTMLString:html baseURL:nil];

2) using removeAllCachedResponses on various places in the above code

 [[NSURLCache sharedURLCache] removeAllCachedResponses];

3) setSharedURLCache did also not provide relief ( I also used this in the AppDelegate applicationDidFinishLaunching).

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

.
.

Unfortunately none of this has helped to "clear the cache" or to free memory allocated by CFData(store).

If anyone could shine some light on this and let me know what I'm missing or doing wrong I would greatly appreciate this.

Thanks a lot for your help!
Ralph
.
.

Edit:
After the initial reply from KiwiBastard I added a screen shot that shows what I observe in Instruments:

Instruments

A: 

I think what could be happening is that your Browser is never deallocated, and the viewDidUnload is probably never being called.

Because your MainViewController has a variable of type BrowserViewController that is never released, that will be resident for the life of your app. Also because you are only switching views, the view will stay in memory too.

Can I suggest you try creating the BrowserViewController variable when you need it, and release it once it has been pushed by the navcontroller eg

 BrowserViewController *browViewController = [[BrowserViewController alloc]     initWithNibName:@"BrowserViewController" bundle:nil];

 browViewController.navigationItem.hidesBackButton = YES;

 [((myAppDelegate *)[UIApplication sharedApplication].delegate).navController setNavigationBarHidden:YES animated:NO];

 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration: 1];
 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:
  ((myAppAppDelegate *)[UIApplication sharedApplication].delegate).navController.view cache:YES];

 [((myAppAppDelegate *)[UIApplication sharedApplication].delegate).navController pushViewController:browViewController animated:NO];
 [UIView commitAnimations];
 [browViewController release];

I know that it will slightly effect performance because it has to load the nib everytime, but you distinctly don't want to cache the vc anyway?

KiwiBastard
I set a break point on viewDidUnload and it is being called. Nevertheless I tried your suggestion of creating the BrowserViewController variable when needed, and release it once it has been pushed by the navcontroller. This has unfortunately not solved this issue.
Ralph
I added two screen shots that show what I observe in Instruments. Thanks for the suggestion above. Hope you or someone else will have another idea.
Ralph