如何解决这个内存泄漏警告

时间:2022-08-17 08:58:24

I got the following code line:

我有以下代码行:

mainLayer.shadowColor = CGColorCreate( CGColorSpaceCreateDeviceRGB(), components );

When I run Product->Analyse in xcode it gives me the warning:

当我在xcode中运行Product->分析时,它会给我一个警告:

Potential leak of an object allocated on line 176

So that means that I do not free my CGColor. Therefore I thought a good solution would be the following:

这意味着我没有释放CGColor。因此我认为一个好的解决办法是:

CGColorRef shadowColor = CGColorCreate( CGColorSpaceCreateDeviceRGB(), components ); 
mainLayer.shadowColor = shadowColor;
CGColorRelease( shadowColor );

But I still get the same leak warning. How do I repair the problem?

但我还是得到了同样的泄漏警告。我如何修复这个问题?

2 个解决方案

#1


16  

You need also to release colorspace:

您还需要释放colorspace:

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorRef shadowColor = CGColorCreate( colorspace, components ); 
mainLayer.shadowColor = shadowColor;
CGColorRelease( shadowColor );
CGColorSpaceRelease(colorspace);

#2


1  

Is this:

是这样的:

CGColorSpaceCreateDeviceRGB()

by any change returning an object you're responsible for deallocating? I thought I remembered there being a function like CGColorSpaceRelease().

通过任何更改返回一个对象,您负责重新分配?我记得有一个函数像CGColorSpaceRelease()。

#1


16  

You need also to release colorspace:

您还需要释放colorspace:

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorRef shadowColor = CGColorCreate( colorspace, components ); 
mainLayer.shadowColor = shadowColor;
CGColorRelease( shadowColor );
CGColorSpaceRelease(colorspace);

#2


1  

Is this:

是这样的:

CGColorSpaceCreateDeviceRGB()

by any change returning an object you're responsible for deallocating? I thought I remembered there being a function like CGColorSpaceRelease().

通过任何更改返回一个对象,您负责重新分配?我记得有一个函数像CGColorSpaceRelease()。