Search code examples
iospdfxcode4uiwebview

Memory warning when loading a PDF in UIWebView


I have 7 ViewControllers in an app, one of which loads a local PDF into a UIWebView. The initial load has no problems, but if I leave that ViewController and come back to it, upon the second or third time I get "Received memory warning" while debugging on an iPad (I haven't experienced this problem in the simulator, but that doesn't mean I won't get it eventually). I have made sure the case/caps are consistent with the file and my code, and I get no build errors or warnings. I have been told mixed stuff about xcode4 needing @property/@synthesize/dealloc, so I've tried both with and without that, and the results haven't changed.

Here is my .h file

#import <UIKit/UIKit.h>

@interface Article6 : UIViewController {
    IBOutlet UIWebView *webview;
}

@end

and here is my .m file

#import "Article6.h"

@implementation Article6


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

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"mfa" ofType:@"pdf" inDirectory:NO];
    NSLog(@"path: %@",path);
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webview loadRequest:request];

}


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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

@end

I am using the swipe gesture from the storyboard, and also a regular button, and this error occurs with both segues. And yes, all the outlets are connected.


Solution

  • Check out your own comments in your viewDidUnload method. This is where you need to release, it appears that you are creating a new view everytime you load a view. So release it when you unload. I'm guessing deAlloc doesn't get called every time you close a webview.

    To make sure I'd simply just use some logs to see your retain count and make sure you are keeping it down. Good luck!