开发者

Trying to understand memory management on the iOS platform

开发者 https://www.devze.com 2023-01-12 12:47 出处:网络
Here\'s a block of code that has leaks... NSString *filename = [NSString stringWithFormat:@\"%@.png\", sketchID];

Here's a block of code that has leaks...

NSString *filename = [NSString stringWithFormat:@"%@.png", sketchID];
CGImageRef imageRef = CGBitmapContextCreateImage(paintView.canvas.mBitmapContext);
UIImage* image = [[UIImage alloc] initWithCGImage:imageRef];
NSData* imag开发者_运维技巧eData = UIImagePNGRepresentation(image); 
  1. Where are they?
  2. In general, how can I tell so I don't create leaks in the future?
  3. What's the proper way to fix them?

Thanks so much!


AS far as I can tell you have memleaks in:

CGImageRef imageRef = CGBitmapContextCreateImage(paintView.canvas.mBitmapContext);

You need to call CGContextRelease. Check this SO question out.

You need to release image aswell. After creating imageData, do:

[image release];

You don't have to release fileName since you are not explicitly allocating memory for it. It will autorelease when variable falls out of scope. There are naming conventions in objective-c that will tell you when you will have to release and when you don't. Check out Apple documentation for it.

Hope it helps.


The general rule is that when you call alloc then you need a corresponding release. The exception is when you call autorelease. Also, when you use convenience methods, such as 'stringWithFormat'.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号