Can someone please help me understand why the static analyzer says there is a potential leak at the line with the for(NSDictionary…)?
- (void)imageSearchController:(id)searchController gotResults:(NSArray *)results
{
if ([results count] == 0) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:nil
message:@"I was not able to find anything! Please try again."
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease];
[alertView show];
} else {
[self increaseSearchIndex];
for (NSDictionary *item in results) {
[imageGallery addSubview:[[ImageBox createImageBoxWitImageURL:[item objectForKey:@"tbUrl"]] retain]];
}
if (searchIndex <= 60) {
[imageGallery addSubview:buttonBox];
} else {
[buttonBox removeFromSuperview];
}
//position the images with respect to each other and screen orientation
[self positionImages];
}
[activityIndicator stopAnimating];
}
- (void)clearImages
{
for (UIView *subview in [imageGallery subviews]) {
if ([subview isMemberOfClass:[ImageBox class]] || [subview isMemberOfClass:[ButtonBox class]]) {
[subview removeFromSuperview];
}
}
}
The image box returns the image with padding to the other other methods above. I’m new to memory management since I’ve been using ARC in the past. Please let me know if you see other potential leaks. I used the Leaks Instrument tool and it says there are no leaks! But I’m not sure that’s the case since I tried to introduce a leak and it still said there were none. Below is all ImageBox Code:
@interface ImageBox : UIView
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImage *image;
+ (id)createImageBoxWitImageURL:(NSString *)imageURL;
@end
@implementation ImageBox
@synthesize imageView;
@synthesize image;
- (id)initWithImageURL:(NSString *)imageURL
{
self = [super init];
if (self) {
int padding = 10;
int imageHeight = 108;
int imageWidth = 108;
int paddingBoxHeight = imageHeight + (2 * padding);
int paddingBoxWidth = imageWidth + (2 * padding);
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];
image = [[[UIImage alloc] initWithData:imageData] autorelease];
[imageData release];
imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 13.0;
imageView.contentMode = UIViewContentModeScaleAspectFill;
CGRect imageFrame = CGRectMake(padding, padding, imageWidth, imageHeight);
[imageView setFrame:imageFrame];
CGRect paddingBoxFrame = CGRectMake(0, 0, paddingBoxWidth, paddingBoxHeight);
[self setFrame:paddingBoxFrame];
[self addSubview:imageView];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
+ (id)createImageBoxWitImageURL:(NSString *)imageURL
{
ImageBox *imageBox = [[self alloc] initWithImageURL:imageURL];
return [imageBox autorelease];
}
- (void)dealloc
{
[imageView release];
[image release];
[super dealloc];
}
you are over-retaining the retaining the object, as it is already owned by the dictionary you are storing it in. if you would remove it form the dictionary, but still wants to have around, than you should retain it or use a retaining property.
try:
UIView’s
-addSubview:retains the subview:You are over-releasing image, imageView, as you release them in dealoc, but also call autorelease on them.