分配对象的潜在泄漏 - 不确定原因

时间:2012-05-29 00:06:13

标签: objective-c ios memory-management memory-leaks

有人可以帮助我理解为什么静态分析仪说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];
        }
    }
}

图像框将带有填充的图像返回到上面的其他方法。自从我过去使用ARC以来,我对内存管理不熟悉。如果您发现其他可能的泄漏,请告诉我。我使用了泄漏仪器工具,它说没有泄漏!但我不确定是不是因为我试图引入泄漏,但它仍然说没有泄漏。以下是所有ImageBox代码:

@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];
}

2 个答案:

答案 0 :(得分:2)

这是因为有一个明确的,不必要的retain

[imageGallery addSubview:
  [[ImageBox createImageBoxWitImageURL:[item objectForKey:@"tbUrl"]] retain]];
                                                                     ^^^^^^

答案 1 :(得分:1)

[imageGallery addSubview:[[ImageBox createImageBoxWitImageURL:[item objectForKey:@"tbUrl"]] retain]];

你过度保留了保留对象,因为它已经被你存储的字典所拥有。如果你要将它从字典中删除,但仍然想拥有,那么你应该保留或使用保留财产。

尝试:

[imageGallery addSubview:[ImageBox createImageBoxWitImageURL:[item objectForKey:@"tbUrl"]]];

UIView的-addSubview:保留了子视图:

  

<强>讨论
  此方法保留视图并将其下一个响应者设置为   接收器,这是它的新超视图。

当你在dealoc中释放它们时,你会过度释放image,imageView,但也会对它们进行自动释放。