objective-c - 分配的对象的潜在泄漏 - 不知道为什么

标签 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,所以我对内存管理很陌生。如果您发现其他潜在的泄漏,请告诉我。我使用了 Leaks Instrument 工具,它说没有泄漏!但我不确定情况是否如此,因为我试图引入泄漏,但它仍然说没有泄漏。以下是所有 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];
}

最佳答案

这是因为存在显式的、不必要的retain

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

关于objective-c - 分配的对象的潜在泄漏 - 不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791656/

相关文章:

ios - 当 block 内的 ivar 可能被释放时,管理内存的最佳方法是什么?

ios - 从 Nib 加载的 subview 中的文本字段访问文本

objective-c - 使用完成按钮以编程方式添加 UINavigationBar

ios - 在启动时显示自定义键盘

ios - NSUserDefaults 无法检索图像数组

delphi - 何时以及为何在 DirectShow 过滤器中使用 CoTaskMemAlloc()?

iphone - Objective-C run loop停止和重启的方法?

ios - 如何在 iOS 中提供按钮退出功能

ios - 当键盘处于事件状态时,如何阻止 UITableView 在重新加载时不恰本地更改弹出窗口中的大小?

swift - 在一个类中引用一个类的实例数组会产生强引用循环吗?