内存泄漏在哪里 [英] where is the memory leak
问题描述
我有一行代码:
CGFloat *components = CGColorGetComponents([color CGColor]);
并且分析仪说这里有泄漏. 我尝试了free(components),但是当我这样做时应用程序崩溃了. 因为我没有分配任何东西,这里真的有泄漏吗?我知道CGColorGetComponents返回一个由3个CGFloat组成的数组,但是当我尝试释放它们时会崩溃.
and the analyzer is saying that there is a leak here. I tried free(components) but the app crashes when I do that. Is there really a leak here since I didnt allocate anything. I know that CGColorGetComponents returns an array of 3 CGFloat's but when I try to free them it crashes.
感谢您的帮助.
推荐答案
请参见 SO#792119 .
声明变量不会创建数组.实际上,CGColorGetComponents都没有.不管创建CGColor对象是什么,它都会创建数组并将其存储在对象中. CGColorGetComponents使您可以找到该存储的指针.
Declaring the variable does not create the array. In fact, neither does CGColorGetComponents. Whatever created the CGColor object created the array and stored it inside the object; CGColorGetComponents lets you have the pointer to that storage.
简而言之,不要释放指针.
In short, do not free your pointer.
但是,您可以将其设置为NULL.
You could, however, set it to NULL.
这篇关于内存泄漏在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!