开发者

Memory leak with NSData

开发者 https://www.devze.com 2022-12-29 00:24 出处:网络
I\'m having a leak with this code without being able to find where it\'s coming from. This function get called within an autorelease pool.

I'm having a leak with this code without being able to find where it's coming from. This function get called within an autorelease pool. I release the IplImage* image argument.

When I run the ObjAlloc tool, it tells me that "NSData* data" is leaking. If I try to manually release the UIImage returned by this function, I get an EXC_BAD_ACCESS error, probably because this UIImage is autoreleased.

I'm a bit confused, any hint would be appreciated.

Thanks!

UIImage *UIImageFromIplImage(IplImage *image)
    {
 NSLog(@"IplImage (%d, %d) %d bits by %d channels, %d bytes/row %s", image->width, image->height, image->depth, image->nChannels, image->widthStep, image->channelSeq);

 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
 NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize];
 CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
 CGImageRef imageRef = CGImageCreate(image->width, image->height,
          image->depth, image->depth * image->nChannels, image->widthStep,
          colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault,
          provider, NULL, false, kCGRenderingIntentDefault);
 UIImage *ret = [UIImage imageWithCGImage:imageRef];
 CGImageRelease(imageRef);
 CGDataProviderRelease(provider);
 C开发者_运维百科GColorSpaceRelease(colorSpace);
 return ret;
    }


The code looks good to me. Maybe you could try to verify if it’s a real leak, say by running the code in a loop to see if the memory usage goes up?


Thanks for your answers. You are all right and this specific code doesn't leak.

The reason for the leak is that this UIImage returned was set as this image of an UIImageView. However I didn't release this UIImageView because I thought that since it was loaded from a nib, it would be released automatically.

Could the reason be that I have a property set to retain for this UIImageView?

@property (nonatomic, retain) UIImageView* imageView;

Should I have a property for objects created from a nib file?

Thanks!

0

精彩评论

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

关注公众号