1

Im new developer on ObjC and trying to make a fill color app. When I touch on a image, the color will be changed but I got the merory leak with this function need your help:

-(void) updateImageWithColorSelected:(int) pos{
   CGImageRef imageRef = self.basicImage.CGImage;
   NSData *data = CGDataProviderCopyData(CGImagerGetDataProvider(imageRef));//leak here
   Byte *pixels = (Byte *)[data bytes];
   //change color...
   for(int i = 0; i< IMG_SIZE; i++){
     pixels[j] = 255;
   }
   CGDataProvider provider = CGDataProviderCreateWithData( NULL, pixels, [data length], NULL];
   CGImageRef newImageRef = CGImageCreate(w,h....);
   self.basicImage = [UIImage imageWithCGImage:newImageRef];
   //release newImageRef
   CGImagerRelease(newImageRef);

   // set basic image to img
   [self.img setImage:self.basicImage];

   data = nil;
   [data release];
}

I try to remove all the code except NSData *data = CGDataProviderCopyData and the app still leak. Do you guys have any idea how to release "data" ?

Thank you in advance,

}

davidphan
  • 11
  • 2

2 Answers2

1
   // set basic image to img
   [self.img setImage:self.basicImage];

   data = nil;
   [data release];
}

You're sending release to a nil pointer.

   [data release];
   data = nil;
}

This will do better.

Edit: the issue with CGDataProviderCreateWithData

When data is released, the data pointer you passed to CGDataProviderCreateWithData becomes invalid. This is expected. The proper use of this function requires you allocate a buffer for the data and provide a callback to release the data when the provider is released.

The best solution for you is to use CGDataProviderCreateWithCFData instead, taking advantage of the toll-free bridging between Foundation and CoreFoundation objects.

Use:

CGDataProvider provider = CGDataProviderCreateWithCFData( (CFDataRef) data );
  • Hello fabrice truillot de chambri, Thanks, Your advide was help, yes my fault, the app does not leak any more with the change, but the image can not display now. Seem release data mean release imageRef too. Anybody have experienced on this matter? Thanks – davidphan Jul 12 '12 at 08:06
  • Edited my answer with the solution to your other problem. :) – fabrice truillot de chambrier Jul 12 '12 at 09:26
  • Dear friend, Thanks so much to point me out. The solution fixed the leak absolutely. – davidphan Jul 12 '12 at 09:40
0

Note that at present the data provider created by the call to CGDataProviderCreateWithData() or CGDataProviderCreateWithCFData() is also being leaked, and should be released by calling CGDataProviderRelease(). (This leak is undoubtedly minor compared to the original leaked data.)

rsfinn
  • 1,073
  • 14
  • 26