Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Potential leak of an object stored into

I'm analyzing this block of code from an SDK and basing the kind of error on the answer of my latest question:

How to release correctly memory in iOS: Memory is never released; potential leak of memory pointed to by

dasblinkenlight suggested me to create an NSData object for could releasing 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 kind of error on these lines:

potential leak of an object stored into 'myColorspace'

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

potential leak of an object stored into 'myGradient'

UIGraphicsPopContext();

I tried with:

free(myColorspace);
free(myGradient);

but I keep the same kind of issues, what can I do to resolve it

thanks in advance all your support

like image 681
Jesús Ayala Avatar asked Dec 06 '22 06:12

Jesús Ayala


1 Answers

Listen to exactly what the errors are telling you.

"potential leak of an object stored into myColorspace"

Lets look at colorspace and see if we can find the problem. myColorspace is created CGColorSpaceCreateDeviceRGB so retain count is +1, but then never released. This is unbalanced and it needs to be released at the end. We need to add a CGColorSpaceRelease(myColorSpace);

"potential leak of an object stored into myGradient"

Same problem, creation with retain count +1, with no corresponding release. Add a CGGradientRelease(myGradient);

Do not use free on anything created with a framework Create function. The internal structure could be more complex, and free will not properly take care of all the memory. Use a corresponding Release function.

like image 62
Justin Meiners Avatar answered Dec 10 '22 13:12

Justin Meiners