2012-05-29 3 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를 사용해 왔기 때문에 메모리 관리가 처음이다. 다른 누출 가능성이있는 경우 알려주십시오. Leaks Instrument 도구를 사용했는데 누수가 없다고합니다! 그러나 누출을 도입하려고 시도한 이래로 나는 확실하지 않습니다. 아래의 모든 ImageBox 코드 : 명시 적, 불필요한 retain이 있기 때문에

@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에서 이미지를 릴리스 할 때 imageView를 과도하게 릴리즈 할뿐만 아니라 이미지 렌더링을 autorelease라고도합니다.

+0

글쎄, 다소 혼란 스럽네요. (미안 해요. ARC를 사용하면서 메모리 관리가 처음이에요.) ImageBox가 자동 렌더링 된 객체를 반환합니다. 네가 그걸 유지할 필요가 있다고 생각 했어. 또한 보유를 제거하면 응용 프로그램이 충돌합니다. 나중에 내 응용 프로그램 –

+3

에서 removeFromSubview를 시도 할 때 충돌이 발생한다고 가정합니다.보기에 의해 유지됩니다. 그래서 만약 그것이 충돌한다면, U는 다른 곳에서 과다 방출/저 유황을 가져야 만합니다. 아마 ImageBox-calss에서. 코드를 보여주십시오. – vikingosegundo

+0

더 많은 것을 보여주기 위해 위의 코드를 수정했습니다. 새 검색에서 superview에서 제거하여 이미지를 지 웁니다. –

2

그것입니다.

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

아래의 답장을 보시기 바랍니다. –

관련 문제