tags:

views:

361

answers:

1

Hi everybody!

I've got a memory leak i just don't know how to solve.

This is the leaking code:

[newImg release];
CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context =  CGBitmapContextCreate(Data, width, 
                                              height, 
                                              8, 4*width, 
                                              d_colorSpace, 
                                              kCGImageAlphaNoneSkipFirst);
UIGraphicsPushContext(context);
CGImageRef new_img = CGBitmapContextCreateImage(context);
UIImage * convertedImage = [[UIImage alloc] initWithCGImage:
                             new_img];
CGImageRelease(new_img);
CGContextRelease(context);
CGColorSpaceRelease(d_colorSpace);
newImg = convertedImage;

I modify pixel information stored in Data, then with this method i create a UIImage back from the Data (which is as unsigned char array)

The xcode instruments tells me that there are leaks in here:

CGImageRef new_img = CGBitmapContextCreateImage(context);

And here:

UIImage * convertedImage = [[UIImage alloc] initWithCGImage:
                             new_img];

though i release both of them :( Can somebody tell me how to solve this?

Thanks in advance ^-^

A: 

Edit: Original post did not seem to help, so deleting and re-posting.

Try this instead:

//[newImg release]; DELETE THIS LINE
CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context =  CGBitmapContextCreate(Data, width, 
                                              height, 
                                              8, 4*width, 
                                              d_colorSpace, 
                                              kCGImageAlphaNoneSkipFirst);
UIGraphicsPushContext(context);
CGImageRef new_img = CGBitmapContextCreateImage(context);

newImg = [UIImage imageWithCGIImage:new_img];

CGImageRelease(new_img);
CGContextRelease(context);
CGColorSpaceRelease(d_colorSpace);

Have tested this and it does not leak on my end.

pheelicks
uhm.. won't this make newImg a null Pointer? If you release convertedImage, since it was just created i guess the retain count will go down to 0 as soon as you release it, and since newImg points to convertedImage, when convertedImage is released so does newImg, right?Maybe i need to understand these points a little bit more.. I come from Java programming.. :(
XelharK
I tried that code, and it actually leads to a null pointer exception.. So.. Can anybody help me please? :(
XelharK