views:

161

answers:

2

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);
 CGColorSpaceRelease(colorSpace);
 return ret;
    }
+1  A: 

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?

zoul
Thanks for the hint, I'm checkig that.
Kamchatka
A: 

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!

Kamchatka
Loading properties from nib is no magic. “Waking up” an object from nib is simply creating an instance and setting all the connections that were established in the Interface Builder. If you have an accessor created for the connection, the nib loading code will use it. That’s all. This means that if you have a retained property declared for the image view, you own the view and are responsible for releasing it.
zoul
OK, this is getting much more clear in my mind. Thanks!
Kamchatka