iPhone development - preventing leaks

2019-04-09 21:58发布

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.

4条回答
三岁会撩人
2楼-- · 2019-04-09 22:10

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楼-- · 2019-04-09 22:16

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-09 22:24

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

查看更多
成全新的幸福
5楼-- · 2019-04-09 22:25

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?

查看更多
登录 后发表回答