title: General-Block 56, 1024, 8, 244, 24 Memory Leaks using UIWebView loadRequest
body: <p>I'm getting memory leaks that i can't figure out through leaks, build/analyze or overall inspection how to repair. I have a very strong notion that it's due to the loadRequest command from my UIWebview loading javascript, but I can't figure out what's wrong.</p>
  <p>Here's my setup:</p>
  <p>I have a tabbarcontroller that i programatically create with 4 view controllers: a table view, a mapview, another table view and a webview, respectively.</p>
  <ul>
  <li>the 1st tableview shows static data.</li>
  <li>the mapview shows annotations that when clicked populates a variable of the 4th tab, the webview, and switches to the webview to display a locally stored html/javascript file that is a 3d model</li>
  <li>the 2nd tableview is a listing of all the same information each annotation in the mapview represents, just in table form. it has the same functionality in terms of populating the variable, switching and displaying the html/javascript file in the webview</li>
  <li>the webview displays the html/javascript file loaded, if not it shows a UIAlertView to select one from the mapview or the list</li>
  </ul>
  <p>when i finally get to executing the loadRequest() from the webview, initially everything works fine. however, when i begin to switch between tabs and view different 3d models (i.e. go to the map view click an annotation to show say model x, watch it load in the webview, then switch to the listing table and click a row to show model y (or x too) and watch it load in the webview) i start receiving memory leaks on the ipad, not the simulator. these memory leaks are almost always: General-Block 56, 1024, 8, 244 &amp; 24 and have no responsible frames, libraries or stack traces to follow up with.</p>
  <p>in the webview if i comment out the loadrequest line or do a loadrequest with the request object = nil, i have no memory leaks, but when i switch to using a url object that points to the local files (i even tried pointing it to google.com, which has javascript) it erupts in memory leaks.</p>
  <p>i've attempted to do each of these:</p>
  <ul>
  <li>[self.webView loadRequest:nil];</li>
  <li>[self.webView loadHTMLString:@&quot;&quot; baseURL:nil];</li>
  <li>[self.webView stopLoading];</li>
  </ul>
  <p>in viewWillDisappear of my webViewController in attempt to completely clean out the webview for reuse in the future, but it doesn't seem to be doing much help. i've also attempted doing this prior to doing the loadRequest but i get the same memory leaks.</p>
  <ul>
  <li>fyi, i'm running the latest xcode 4.0.2 and the ipad has the latest update, 4.3.2</li>
  <li>from a previous post i commented on and haven't heard back from (<a href="https://stackoverflow.com/questions/5470121/generalblock-56-memory-leak-when-calling-loadrequest-in-uiwebview/5736448#5736448">general-block memory leak</a>) i believe the problem has to do with the fact that webview doesn't know how to fully rid itself of the javascript when i switch views</li>
  <li>i did a test with a simple html file with no javascript and the general-block memory leaks are gone. however, i get other cfnetwork / http message memory leaks but that's a different story and not of my concern at the moment.</li>
  </ul>
  <p>below is example code of when one of the annotations on the mapView is clicked which triggers the loadModel function in the webViewController:</p>
  <pre><code>- (void) mapCallOutPressed: (UIView *) sender {
  
  NSInteger selectedIndex = sender.tag;
  MyLocation *selectedObject = [_mapView.annotations objectAtIndex:selectedIndex];
  
  MyModel *model = selectedObject.model;
  
  NSLog(@&quot;Model selected from mapview = %@&quot;, model.description);
  
  // Get reference to the webview from the tabBarController (viewController's index=3)
  WebViewController *webViewController = [self.tabBarController.viewControllers objectAtIndex:3];
  webViewController.model = model;
  
  [webViewController loadModel];
  
      // Switch tabs to the webView
  self.tabBarController.selectedIndex = 3;
  [self.tabBarController.selectedViewController viewDidAppear:YES];
  </code></pre>
  <p>below is my webViewController.h &amp; .m:</p>
  <p>WebViewController.h:</p>
  <pre><code>WebViewController : UIViewController &lt;UIWebViewDelegate&gt; {
  
  UIWebView *webView;
  NSString *templateRunFilePath;
  MyModel *model;
  NSString *templateRunTitle;
  
  @property (nonatomic, retain) UIWebView *webView;
  @property (assign) MyModel *model;
  @property (nonatomic, copy) NSString *templateRunFilePath;
  @property (nonatomic, copy) NSString *templateRunTitle;
  
  -(void) loadModel;
  </code></pre>
  <p>WebViewController.m:</p>
  <pre><code>- (void) viewDidLoad {
  [super viewDidLoad];
  NSLog(@&quot;in webview did load&quot;);
  
  self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
  self.webView.delegate = self;
  
  self.webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
  self.webView.scalesPageToFit = YES;
  
  [self.view addSubview:self.webView];
  
  }
  
  
  - (void) viewWillAppear:(BOOL)animated {
  
  if (self.model) {        
      self.tabBarController.navigationItem.title = [NSString stringWithFormat: @&quot;%@ - %@&quot;, self.tabBarController.navigationItem.title, self.model.description];
      
  }
  else {
      UIAlertView *msg = [[UIAlertView alloc] initWithTitle:@&quot;No Model Selected&quot; 
                                                    message:@&quot;Please select a model from the map or list tab to view.&quot; 
                                                   delegate:nil 
                                          cancelButtonTitle:@&quot;OK&quot; 
                                          otherButtonTitles:nil];
      [msg show];
      [msg release];
  }
  }
  
  - (void) loadModel {
      
  NSString *localURLPath = [NSString stringWithFormat:@&quot;%@/%@/%@&quot;, self.templateRunFilePath, self.model.folderName, self.model.modelFileName];
  
  NSURL *url = [NSURL fileURLWithPath:localURLPath];
  NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
  [self.webView loadRequest:requestObj];
  }
  
  
  - (void)webViewDidFinishLoad:(UIWebView *)webView {   
  
  [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@&quot;WebKitCacheModelPreferenceKey&quot;];
  }
  
  
  -(void)viewWillDisappear:(BOOL)animated {
  NSLog(@&quot;webview view will disappear&quot;);
  
  [super viewWillDisappear:animated];
  
  //[self.webView loadRequest:nil];
  //[self.webView loadHTMLString:@&quot;&quot; baseURL:nil];
  
   self.webView.delegate = nil;
   }
  
  
  - (void)dealloc {
      self.webView.delegate = nil;
  [self.webView release];
  
  [super dealloc];
  }
  </code></pre>
  <p>if you have any advice or corrections, i'd greatly appreciate it. i have &lt; 1 week to figure this out and would highly thank you if you could give me any insight.</p>
  <p>thanks!</p>
  <p>-Mike</p>