在iPhone中加载应用程序时出现内存泄漏 [英] Memory leak when application loads in iPhone

查看:52
本文介绍了在iPhone中加载应用程序时出现内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于导航的模板,当我使用Instruments运行我的应用程序时,第一次发生内存泄漏的原因是:

I have a navigation based template, when I run my application using Instruments, the very first memory leak comes under:

Leak Object: Malloc 128 bytes 
Responsible Library: CoreGraphics 
Responsible Frame: open_handle_to_dylib_path

我不知道此泄漏来自何处,以及如何清除此泄漏.

I don't know where this leak is coming from and how remove this.

如果这是默认泄漏,那么我认为我不必为此担心.但是,如果不是这样,那么我必须找到一种方法来消除泄漏.

If it's a default leak, then I think I don't need to worry about it. But if it's not then I have to find a way to remove the leak.

推荐答案

在deanWombourne上扩展完全正确的答案.

Expanding on deanWombourne perfectly correct answer a bit..

泄漏仪器会向您显示在应用程序的正常运行过程中不会释放的内存(因为没有任何引用).这本身并不是什么大不了的事,当应用程序退出时将被释放.许多框架代码将分配并保留这些很小的内存块.我不知道它们是否是错误或对应用程序的运行至关重要.无论如何,我们必须接受它们是完全正常的.

The Leaks Instrument shows you memory that isn't going to be free'd in the normal course of the app (because there aren't any references to it). This in itself is no biggie, it will be free'd when the app exits. A lot of the framework code will allocate and leave these very small chunks of memory allocated. I have no idea if they are mistakes or essential to how the app runs. Whatever, we must accept that they are completely normal.

泄漏将这些内存块识别为泄漏",这听起来很糟糕,但这并不是仪器可以帮助您识别的泄漏".

Leaks will identify these chunks of memory as 'Leaks' and that sounds bad, but this is not really the 'Leaks' that the instrument is there to help you identify.

真正的"泄漏存在于可以多次运行的代码中,并且分配了一些永远不会释放的内存,因此随着时间的流逝,它将消耗越来越多的内存,直到使用完所有内存,并且您的应用程序将崩溃.

The 'real' leaks are in the code that can be run many times and which allocate some memory that is never freed, so over time will consume more and more memory until all memory is used and your app will crash.

因此,如果您拥有一个应用程序,无论使用了多长时间或使用了哪种应用程序,它都可以在Apple框架中泄漏" 128个字节,而您通常不必担心.

So if you have an app that no matter how long you use it for or no matter how you use it, it 'leaks' 128 bytes in an apple framework you generally don't have to worry.

但是,如果您有一个应用程序说,每次单击按钮,它将分配一个永远不会释放的新字符串-不管该字符串有多少字节-如果用户按下按钮足够多次,这将消耗掉所有可用于应用程序的内存,并最终将其崩溃.这是您需要注意的泄漏.

However, if you have an app that say, every time you click a button it allocates a new string which is never released - no matter how many bytes the string is - if the user pressed the button enough times this would consume all the memory available to app and eventually crash it. This is the kind of leak you need to watch out for.

实际上,泄漏仪器无法分辨出这两种类型之间的区别,但是您必须能够.例如,您可能需要一种单例对象,该对象只有一个实例,并且在整个应用程序生命周期中都必须存在.您可以在应用启动时创建该对象,实际上,您无需释放该对象,当应用退出时,该对象可以被杀死.泄漏会将其标记为泄漏,并且您与之合作的其他一些开发人员认为这意味着您不知道自己在做什么,就会像小孩子一样跑向老板,并说:他在编写真正的泄漏代码,严重变坏".而且您的老板(不是程序员)会认真对待他,因为这听起来确实很糟糕,并且无论如何他还是从著名的大学中获得了2.2的CS成绩,因此他必须知道自己在说什么.实际上,这完全是完全合理的,而且完全符合您的意图.

The leaks instrument realistically can't tell the difference between the two kinds, but you need to be able to. You might want a kind of singleton object, for example, that there is only ever one instance of, and that needs to exist for the entire lifetime of your app. You create the object on app startup and realistically you never need to free this object, it can be killed when the app exits. Leaks will flag it as a leak, and some other developers that you work with who assume that this means you don't know what you are doing will run to your boss like a little child and say "He's writing really leaky code, and that's reeeeally bad". And your boss, who isn't a programmer will take him seriously because it does sound bad and anyway he scraped a 2.2 in CS from a reputable University so he must know what he's talking about. When really it is completely reasonable and exactly what you meant to do.

因此,请使用Leaks工具在您的代码中查找将破坏您的应用程序的错误.不必担心在Apple框架中发现泄漏"的每个字节.

So, use the Leaks instrument to find bugs in your code that will ruin your app. Don't worry about every byte found 'Leaking' in an Apple framework.

这篇关于在iPhone中加载应用程序时出现内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆