Ios: how to solve this memory leak problem

I got the following line of code:

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

When I run Product-> Analyze in xcode, it gives me a warning:

Potential leak of an object allocated on line 176

So this means that I am not releasing my CGColor. So I thought the following would be a good solution:

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

But I still get the same leak warning. How to fix the problem?

+5
source share
2 answers

You also need to free up the color space:

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

It:

CGColorSpaceCreateDeviceRGB()

any change returning the object for which you are responsible for the release? I thought I remember that there was a function like CGColorSpaceRelease ().

+1
source

All Articles