Potential leakage of an object stored in

I am analyzing this block of code from the SDK and basing the error type on the answer to my last question:

How to free memory in iOS: memory is never released; potential memory leak indicated by

dasblinkenlight suggested that I create an NSData object in order to release my uint8_t * bytes ...

But here in this code:

/** * this will set the brush texture for this view * by generating a default UIImage. the image is a * 20px radius circle with a feathered edge */ -(void) createDefaultBrushTexture{ UIGraphicsBeginImageContext(CGSizeMake(64, 64)); CGContextRef defBrushTextureContext = UIGraphicsGetCurrentContext(); UIGraphicsPushContext(defBrushTextureContext); size_t num_locations = 3; CGFloat locations[3] = { 0.0, 0.8, 1.0 }; CGFloat components[12] = { 1.0,1.0,1.0, 1.0, 1.0,1.0,1.0, 1.0, 1.0,1.0,1.0, 0.0 }; CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB(); CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations); CGPoint myCentrePoint = CGPointMake(32, 32); float myRadius = 20; CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint, 0, myCentrePoint, myRadius, kCGGradientDrawsAfterEndLocation); UIGraphicsPopContext(); [self setBrushTexture:UIGraphicsGetImageFromCurrentImageContext()]; UIGraphicsEndImageContext(); } 

I got the same error on these lines:

potential leak of an object stored in "myColorspace"

 CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations); 

potential leak of an object stored in "myGradient"

 UIGraphicsPopContext(); 

I tried:

 free(myColorspace); free(myGradient); 

but I keep the same problems that I can do to solve it

Thank you in advance for your support.

+4
source share
1 answer

Listen to what exactly the errors tell you.

"potential leak of an object stored in myColorspace "

Let's look at the color space and see if we can find the problem. myColorspace is created by CGColorSpaceCreateDeviceRGB , so the value of count is +1, but then never released. This is unbalanced and should be released at the end. We need to add a CGColorSpaceRelease(myColorSpace);

"potential leak of an object stored in myGradient "

The same problem, creating with saving count +1, without the corresponding version. Add CGGradientRelease(myGradient);

Do not use free for anything created using the Create framework function. The internal structure may be more complex, and freedom will not properly take care of all memory. Use the appropriate Release function.

+14
source

Source: https://habr.com/ru/post/1495593/


All Articles