문제

When I analyze my iPhone app I have a blue arrow warning which says:

potential leak of an object stored into pauseImgBg

I simply crop a partial image from an atlas and put it into the pauseMenuIV imageView.

CGImageRef pauseImgBg = CGImageCreateWithImageInRect([UIImage imageNamed:@"atlas.png"].CGImage, CGRectMake(1, 1, 640.0f, 960.0f));
pauseMenu = [UIImage imageWithData:UIImagePNGRepresentation([UIImage imageWithCGImage:pauseImgBg])];// I have the blue arrow in this line
[pauseMenuIV setImage:pauseMenu];

What could be the problem ?
Thanks.

도움이 되었습니까?

해결책

CGImageCreateWithImageInRect returns a CGImageRef with a retain count of +1. So you must release the recevied CGImageRef.

CGImageRelease(pauseImgBg);

in the end. Try and read the documentation when you have the time. It will help you to isolate these kinds of cases. From documentation

Core Foundation functions have names that indicate when you own a returned object:

  • Object-creation functions that have “Create” embedded in the name
  • Object-duplication functions that have “Copy” embedded in the name.

If you own an object, it is your responsibility to relinquish ownership when you have finished with it.

And from the documentation of CGImageCreateWithImageInRect itself

The resulting image retains a reference to the original image, which means you may release the original image after calling this function.

다른 팁

you'll probably need to release it via

CGImageRelease(pauseImgBg);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top