本文介绍了iPhone开发-防止泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Leaks运行我的应用程序并查看扩展细节中是否有任何泄漏时,它会将我带到代码中的特定行,但是之后我不知道该怎么办!

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!

例如,泄漏在此行显示一个malloc

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.

谢谢.

关于我之前遇到的问题,NSZombieEnabled确保没有对象被释放,这会增加内存使用量.因此,在使用Leaks进行测试时,请确保从您的应用中删除了此设置.认为这可能会对某人有所帮助.

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.

推荐答案

找到了答案.弗雷德里克(Fredrik)的回应让我开始思考.我正在创建NSURLResponse和NSError的实例,然后将其传递给sendSynchronousRequest方法.根据iPhone的内存管理文档,不应执行此操作.我们只需要将对NSURLResponse和NSError对象的引用传递给该方法,该方法将负责创建和释放对象.

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.

这篇关于iPhone开发-防止泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:40