akhil
akhil

Reputation: 11

Help finding memory leak

Can any one help me to find the memory leak in the below code, which adjusts the brightness of an image?

+(NSImage *)brightness:(NSImage *)image andLevel:(int)level
{
    CGImageSourceRef source= CGImageSourceCreateWithData((CFDataRef)[image TIFFRepresentation], NULL);
    CGImageRef img =  CGImageSourceCreateImageAtIndex(source, 0, NULL);


    NSSize size = image.size; 
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); 
    //getting bitmap data from receiver's CGImage
    CFDataRef dataref=CGDataProviderCopyData(CGImageGetDataProvider(img));
    //getting bytes from bitmap image
    UInt8 *data=(UInt8 *)CFDataGetBytePtr(dataref);
    //getting length
    int length=CFDataGetLength(dataref);
    // Perform operation on pixels
    for(int index=0;index<length;index += 1)
    {
        // Go For BRIGHTNESS
        for(int i=0;i<3;i++)
        {
            //printf("This pixel is:%d",data[index + i]);
            if(data[index+i]+level<0)
            {
                data[index+i]=0;
            }
            else
            {
                if(data[index+i]+level>255)
                {
                    data[index+i]=255;
                }
                else
                {
                    data[index+i]+=level;
                }
            }
        }
    }

    // .. Take image attributes
    size_t width=CGImageGetWidth(img);
    size_t height=CGImageGetHeight(img);
    size_t bitsPerComponent=CGImageGetBitsPerComponent(img);
    size_t bitsPerPixel=CGImageGetBitsPerPixel(img);
    size_t bytesPerRow=CGImageGetBytesPerRow(img);

    // .. Do the pixel manupulation
    CGColorSpaceRef colorspace=CGImageGetColorSpace(img);
    CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(img);
    CFDataRef newData=CFDataCreate(NULL,data,length);
    CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);

    // .. Get the Image out of this raw data
    CGImageRef newImg=CGImageCreate(width,height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorspace,bitmapInfo,provider,NULL,true,kCGRenderingIntentDefault);

    // .. Prepare the image from raw data
    NSImage* newImage = [[NSImage alloc] initWithSize:size];
    //To make the drawing appear on the image instead of on the screen
    [newImage lockFocus];
    //Draws an image into a graphics context.
    CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort],*(CGRect*)&rect, newImg);
    [newImage unlockFocus];
    // .. done with all,so release the references

    CFRelease(source);
    CFRelease(img);
    CFRelease(dataref);
    CFRelease(colorspace);
    CFRelease(newData);
    CFRelease(provider);
    return [newImage autorelease];
}

Upvotes: 0

Views: 1344

Answers (2)

user557219
user557219

Reputation:

You’ve forgotten to release newImg, which you’ve obtained via a Create function. Also, you shouldn’t release colorSpace since you haven’t obtained it via a Create or Copy function and you haven’t retained it.

Upvotes: 2

necixy
necixy

Reputation: 5064

Replace the following code lines:

NSImage* newImage = [[NSImage alloc] initWithSize:size];

with

NSImage* newImage = [[[NSImage alloc] initWithSize:size] autorelease];

and this one: return [newImage autorelease]; with return newImage;

I'm not 100% sure about this but give it a try, hope it might help.

:)

Upvotes: 0

Related Questions