Correctly releasing memory

Discussion in 'iOS Programming' started by beardedpig, Mar 10, 2009.

  1. beardedpig macrumors member

    Dec 17, 2008
    I have a help web view which is displayed as a result of tapping the right nav button on a navigation control.

    When the app starts the mem usage is at 10.55mb which increases to 14.00 when the help web view is displayed.

    However when the help web view closes the mem usage only goes back to 13.88?

    If I continue to open and close the help the mem usage slowly creeps up.

    I therefore think I have not got the clean up working correctly for the help viewcontroller.

    Here is the .h and .m files for the help

    #import <UIKit/UIKit.h>
    @interface HelpViewController : UIViewController <UIWebViewDelegate>
    	NSString *htmlContent;
    	UIWebView *webView;
    // setup properties
    @property (assign, nonatomic) NSString *htmlContent;
    @property (assign, nonatomic) UIWebView *webView;
    I used asssign rather than retain as I have seen that this helps to not hold on to items when not required to do so.

    #import "HelpViewController.h"
    #import "KPAppDelegate.h"
    @implementation HelpViewController
    @synthesize htmlContent;
    @synthesize webView;
    -(void) viewDidLoad
    	// always call the base method!
    	[super viewDidLoad];
    	CGRect webFrame = [[UIScreen mainScreen] applicationFrame];  
           // flush them first in case they are hanging around in memory
    	[webView release];
    	webView = nil;
    	webView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];  
    	webView.backgroundColor = [UIColor grayColor];  
    	[webView loadHTMLString: htmlContent baseURL: [NSURL URLWithString: @""]];  
    	[self.view addSubview: webView];  
    	// set nav bar title
    	self.title = @"Help";
    	// reference the app delegate to access isPPLVisible 
    	KPAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    	// add a right button to the navigation controller
    	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"Done" 
    																			   style: UIBarButtonItemStylePlain 
    																			  target: appDelegate
    																			  action: @selector(backToRoot)] autorelease];
    - (void) didReceiveMemoryWarning 
    	 Asks the delegate for the view to scale when zooming is about to occur in the scroll view. This method is optional
        [super didReceiveMemoryWarning]; 
    -(void) dealloc 
    	// dump super
        [super dealloc];

    the method appDelegate.backToRoot simply pops the view off the stack.

    My understanding tell me that I have this setup right but cannot understand the hold on memory plus slow creep usage

    any tip please?

  2. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    Unless you have a good reason then the UIWebView property should be defined as (retain) and the NSString as (copy) not assign. They should both then be released in the dealloc method. If you don't explicitly retain these objects they will disappear and the pointers to them will point to garbage if you access them later on.

    Your viewDidLoad method is a bit odd. The first release of webView is incorrect and shouldn't be there.

    This line
    	webView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];  
    Should be
    	self.webView = [[UIWebView alloc] initWithFrame:webFrame];  
    And seeing as you've gone to the trouble of defining your ivars as properties you might as well use them by refering to webView as self.webView.
  3. beardedpig thread starter macrumors member

    Dec 17, 2008
    Noted, thanks will make amendments and see what occurs!

    Hope you can add some input on my new thread re image memory usage I am just about to add.


Share This Page