views:

590

answers:

5

I'm running leaks through Instruments on my iPhone app and I'm seeing a lot of leaks that don't appear to be coming from my code.

For example:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request  
                                                              delegate:operation];
operation.urlConnection = connection;
[connection release];

Leaks is telling me that the first line is leaking 1008 bytes. That seems to be a pretty standard alloc init with a release. Other leaks that are mentioned are in UIKit and WebKit.

Is it possible that these leaks are in fact in Apple's frameworks, or is more likely my code and leaks isn't showing the information accurately?

+4  A: 

It's entirely possible that Apple's frameworks have leaks in them (however unlikely it may seem) - there were several in the Core Data implementation for iPhone in the 3.0 GM release.

What you should do when you suspect such a thing is try to find a sample project from Apple that uses the functionality you're looking at or reduce your own code as much as possible (maybe build a minimal side project), then test that with Instruments. If you can reproduce the leak reliably, submit a bug to Apple.

Tim
Thanks, I will give that a try.
Brian
A: 

Besides that, have you tried to test the app on device instead of simulator? Running instruments on simulator is not very accurate and reliable. Try this one as well http://www.tuaw.com/2009/09/08/xcode-3-2-daily-tip-analyzing-your-code/

Nguyen Khang
I use the static analyzer. It doesn't say anything about these leaks. Can you run Leaks on the device. If so I'm not sure how to do that.
Brian
Nevermind - I got it working. Thanks.
Brian
A: 

Do you keep a reference to your delegate object anywhere else?

If you think about it, Leaks would assume delegate is a leak if you have no other references to it around but it is still retained. Also, how are you freeing your delegate (named operation) when the request is done?

Kendall Helmstetter Gelner
I did not think about the delegate portion being the leak. I am not too familiar with Leaks so I thought it would see that. I didn't realize that it depended on reference counting. This code is generated from the WSDL2OBJC project. I'll have to look into it some more. Thanks.
Brian
+3  A: 

Are you running with NSZombieEnabled? That will cause fake "leaks" to show up in Instruments.

nall
Yes I am - didn't know about that one. Thanks.
Brian
+1  A: 

I think this is where your leak stems:

operation.urlConnection = connection;

You may not be doing proper memory management in operation.

ahmet emrah