iPhone development - preventing leaks

2019-04-09 22:28发布

问题:

When I run my app with Leaks and view the Extended Details for any of the leaks, it takes me to a particular line in my code, but I don't know what to do after that!

For instance, Leaks shows a malloc at this line

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

But I do not know what the problem is in the statement! Can someone please tell me how to interpret such problems and avoid leaks.

Thanks.

Edit: Regarding the previous question I had, NSZombieEnabled makes sure no objects are deallocated and this increases the memory usage. So when testing with Leaks, make sure this setting is removed from your app. Thought this might help someone.

回答1:

Found the answer. Fredrik's response got me thinking. I was creating an instance of NSURLResponse and NSError which I was then passing to the sendSynchronousRequest method. According to the memory management document of the iPhone, this should not be done. We just need to pass a reference to the NSURLResponse and NSError objects to the method and the method takes care of creating and releasing the objects.

Hope that helps someone else. Thanks a lot for the answers everyone.



回答2:

The Extended Detail pane will give you stack traces showing you the stack at the leak. Generally a good place to start is to look at your methods & the last method of your code in the stack and see what you are doing memory wise there, it sounds like you could be over retaining an object. Start there for now



回答3:

Do you free the response and error objects after the call? Those are possibly allocated in the call.



回答4:

A leak in the method you mention above was supposed to be fixed for the iPhone OS 2.2 release. Which version of the iPhone OS are you using?