views:

625

answers:

2

I have already found a lot of information about how to solve memory leaks for iPhone Obj C code. The last two leaks keep me puzzled, I'm probably overlooking something. Maybe you can spot it.

Instruments reports 2 leaks for the following code (part of a UIViewController subclass):

(1)  UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0,
                          0.0,
                          self.view.bounds.size.width,
                          self.view.bounds.size.height - LOWER_VERT_WINDOW_MARGIN)];
(2)  webView.scalesPageToFit = YES;
(3)  webView.dataDetectorTypes = UIDataDetectorTypeNone;
(4) 
(5)  NSURL *url = [NSURL fileURLWithPath:self.fullPathFileName isDirectory:NO];
(6)  NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
(7)  [webView loadRequest:urlRequest];
(8)  [urlRequest release], urlRequest = nil;
(9)  [self.view addSubview:webView];
(10) [webView release], webView = nil;

Instruments claims 128 bytes are leaking in line 1, as well as 256 bytes in line 4. No idea if it means line 3 or line 5.

Does anybody have a clue what I'm overlooking?

+3  A: 

1) Make sure you are leak testing on a device and not the simulator

2) Failing that, try setting the url cache by adding this to your applicationDidFinishLaunching in your app delegate:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
Mobs
You made my day!Hooked up the device, started Instruments, and sure enough: the leaks above did not occur anymore.I only say two leaks, and my software wasn't involved (not in the stack trace), so no worries there.Thanks! Never knew there was such a difference between the simulator and the device with respect to leaks.
Sander de Jong
I hit this one a couple of months ago too. google that nsurlcache line and you will see some info about leaks with NSURLConnection
Mobs
A: 

totally worked! I have been working on my URL Cache leak since yesterday, I even dreamt about it last night! Ha! anyway, thank you!

skinny123