2012-05-29 104 views
2

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

回答

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

你保持过度的保留的对象,因为它已经被你存储它在字典中拥有。如果你删除它形成的字典,但仍希望有周围,比你应该保留它或使用保留财产。

尝试:

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

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

讨论
这种方法保留看法,并将其下一个响应者的 接收器,这是它的新上海华。

当你在dealoc中释放它们时,你会过度释放image和imageView,但是也会对它们调用autorelease。

+0

好吧,我有点困惑(对不起,我是新来的内存管理,因为我一直在使用ARC)... ImageBox返回一个autoreleased对象。我以为你需要保留这一点。此外,如果我删除保留,我的应用程序崩溃。我假设当我在应用程序 –

+3

中稍后尝试从RemoveSubview中移除它时,会发生崩溃,它被视图保留。所以如果它崩溃,比你必须在其他地方有一个过度释放/欠保留。可能在ImageBox中。请显示代码。 – vikingosegundo

+0

我修改了上面的代码以显示更多内容。在新的搜索中,我通过从超级视图中移除来清除图像 –

2

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

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

请看下面我的回复 –