Reputation: 328
I have a webviewcontroller, which I create one instance of, and then I change the request, and show the webview based on which row is selected in a tableview.
// Replace tableView:didSelectRowAtIndexPath with the following
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
CGRect rect = self.view.frame;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
rect.size.width = width; //self.view.frame.size.width;
rect.size.height = height; //self.view.frame.size.height;
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
page = [[WebViewController alloc] initWithUrl:entry.articleUrl];
page.view.frame = rect;
[self.view addSubview:page.view];
}
My problem is, that after loading a few of these, I get memory warnings and crash.
In another tab, I also have a webview which loads a google map when the tab loads. This crashes the app almost as soon as it is loaded.
Since I am only making one instance of each, I don't see why this problem is happening. Previously I created a new webviewcontroller each time, and then released it, but I had the same problem.
Here is the code to load the Google map:
@interface MapViewController : UIViewController {
IBOutlet UIWebView *mapPage;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
url_string = @"http://maps.google.fr/maps/ms?msa=0&msid=213119412551786463007.0004b3fb7f4c123377402&z=12";
url = [NSURL URLWithString:url_string];
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url_string]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
mapPage.scalesPageToFit = YES;
mapPage.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[mapPage loadRequest:request];
[super viewDidLoad];
}
The UIWebView is created in interface builder.
It runs out of memory and crashes right after the map is finished loading, or as soon as you try to interact with the map. It appears to be the very same problem as above, but this time there are no tabs, or no changing the request that the UIWebView uses.
Upvotes: 2
Views: 3841
Reputation: 328
It turns out the pages were being cached.
In the applicationDidReceiveMemoryWarning method of my application, I added a line to clear the cache. This fixed both of my p
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
Upvotes: 2
Reputation: 29767
Each time when you select row in UITableView you create a new object of WebViewController and add its view (for some reason?) to current controller's view. I'm not sure that you at least remove that view from superview (you also have to release WebViewController). And of course it will take more and more memory.
And what is the reason to create a whole UIViewController and use it as subview? Why you didn't use UINavigationController?
Upvotes: 2