iphone - UIColor泄漏......需要释放物体吗? [英] iphone - UIColor leaking... need to release the object?

查看:67
本文介绍了iphone - UIColor泄漏......需要释放物体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有很多这样的行

I have a bunch of lines like this on my app

UIColor *myColor = [UIColor colorWithRed:corR green:corG blue:corB alpha:1.0];

乐器说这些线路正在泄漏。因为这不是正式的,据我所知,一个alloc操作(不是吗?)我没有看到需要释放对象,但是由于乐器在抱怨,我添加了几行

Instruments are saying these lines are leaking. As this is not formally, as far as I see, an alloc operation (isn't it?) I don't saw the need to release the object, but as instruments are complaining, I added several lines as

[myColor release]

使用变量后,取悦野兽。

after using the variable, to please the beast.

这样做会有问题吗,比如崩溃什么的?

Will I have problems doing this, like crashes or something?

显然这样做可以解决问题,但是我发布一个未分配的对象我感到不舒服。

Apparently doing this is solving the problem, but I am not comfortable to release an object that was not allocated.

你怎么看?

谢谢。

I假设这是一个xcode问题或框架泄漏。
要证明我用

I suppose this is a xcode problem or a framework leak. To prove that I replace the lines with

UIColor *myColor = [[UIColor alloc] initWithRed:corR green:corG blue:corB alpha:1.0];

然后可以安全地释放对象......

and then the object could be safely released...

这样做,解决了问题。

推荐答案

不要释放对象,你不拥有它你最终会崩溃。 UIColor可能只是为你缓存这些颜色,而且仪器无法知道这一点,所以它将它们报告为泄漏(基本上是创建的东西,你没有引用,但还没有被释放)。

Don't release the object, you don't own it and you will eventually get crashes. UIColor is probably just caching these colors for you, and Instruments has no way of knowing this so it reports them as leaks (basically stuff that got created and you don't have a reference to anymore but hasn't been deallocated).

尝试运行仪器一段时间(使用模拟器)然后发送内存警告以查看UIColor是否将清除其缓存。无论哪种方式,都没有什么可以真正解决核心框架内发生的泄漏,所以不要尝试。只是确保你没有以某种方式泄露它们(比如在某些时候保留它们并且从不释放它们)。

Try running instruments for some time (using the simulator) and then sending a memory warning to see if UIColor will purge its cache. Either way, there isn't anything you can really do to fix leaks happening inside core frameworks, so don't try. Just make sure you're not actually leaking them somehow (like retaining them at some point and never releasing them).

这篇关于iphone - UIColor泄漏......需要释放物体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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