2011-11-13 3 views
0

첫 번째 iOS 애플리케이션을 프로그래밍 중이며 비동기 적으로 이미지를 UITableViewCell에로드하려고합니다. 배경에있는 이미지를 NSMutableArray에로드하고, cellForRowAtIndexPath 메소드에서 검색합니다. 세포는 이미지를로드 할 수 있지만 ~ 5 세포에 충돌하고 나에게 두 가지 오류 중 하나가 제공 : 오브젝트 0x6c30e60에 대한 * 오류 : malloc에와 ApplicationCell.m에서 @synthesize 라인에서UITableViewCell에서 비동기 이미지를로드 할 때 행글라이딩

SIGABRT : double free

또는 그냥 gdb를 입력하고 iconView.image = newIcon을 가리 키십시오. (스레드 1). 그것을 synthesize'd @

난 후, 테이블 뷰 컨트롤러의 @interface에있는 NSMutableArray * imageArray을 선언하고 @property로 설정 한 :

@synthesize searchArray; 
... 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    ... 
    self.imageArray = [[NSMutableArray alloc] initWithCapacity:8]; //<-- Am I doing this correctly? 
    for (int i=0; i < 8; i++) { 
     [self.imageArray addObject:[UIImage imageNamed:@"empty.png"]]; 
    } 
    ... 
} 

8의 최대있을 것입니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     [self.cellNib instantiateWithOwner:self options:nil]; 
     cell = tmpCell; 
     self.tmpCell = nil; 
    } 
    UIImage *image; 
    NSUInteger row = [indexPath row]; 

    [cell setItem2: [[searchArray objectAtIndex:row] objectForKey:@"item"]]; 
    [cell setPrice2: cell.price]; 
    image = [imageArray objectAtIndex:row]; 
    NSLog(@"num elements in imageArray = %d, loading: %d",[imageArray count], row); 
    [cell setIcon2: image]; 
[image release]; 
    return cell; 
} 
: 여기
- (void)requestFinishedImage:(ASIFormDataRequest*)request { 
    //image request 
    NSData *responseData = [request responseData]; 
    NSIndexPath *index = [NSIndexPath indexPathForRow:request.tag inSection:0]; 
    UIImage *image = [[UIImage alloc] initWithData:responseData]; 
    NSLog(@"requested Image with tag: %d",request.tag); 
    [imageArray replaceObjectAtIndex:request.tag withObject:image]; 
    ApplicationCell *cell = (ApplicationCell *)[self.tableView cellForRowAtIndexPath:index]; 
    [cell setIcon2:image]; 
    cell.icon = image; 
    [image release]; 
} 

가 cellForRowAtIndexPath이다 imageArray 내에 배치 될 배경 이미지 로딩,

그리고 여기 내 ApplicationCell.h 및 ApplicationCell.m 있습니다

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 

@interface ApplicationCell : UITableViewCell 
{ 

    UIImage *icon; 
    NSString *item; 
    NSString *price; 
    IBOutlet UIImageView *iconView; 
    IBOutlet UILabel *itemLabel; 
    IBOutlet UILabel *priceLabel; 
} 
- (void)setIcon2:(UIImage *)newIcon; 
- (void)setItem2:(NSString *)newItem; 
- (void)setPrice2:(NSString *)newPrice; 

@property(retain) UIImage *icon; 
@property(retain) NSString *item; 
@property(retain) NSString *price; 

@end 

ApplicationCell.m

#import "ApplicationCell.h" 

@implementation ApplicationCell 

@synthesize icon, item, price; 

- (void)setIcon2:(UIImage *)newIcon 
{ 
    [self setIcon:newIcon]; 
    iconView.opaque = YES; 
    iconView.image = newIcon; 
} 
- (void)setItem2:(NSString *)newItem 
{ 
    [self setItem:newItem]; 
    itemLabel.text = newItem; 
} 

- (void)setPrice2:(NSString *)newPrice 
{ 
    [self setPrice:newPrice]; 
    priceLabel.text = newPrice; 
} 


- (void)dealloc 
{ 
    [icon release]; 
    [item release]; 
    [price release]; 

    [iconView release]; 
    [itemLabel release]; 
    [priceLabel release]; 

    [super dealloc]; 
} 

@end 

가이 아이폰 OS 응용 프로그램을 시도 내 처음부터 코드에서 아마 실수가 있지만 내가 배울 수 있도록 알려주세요! 나는 너무 오래이 비동기 로딩 문제에 갇혀 있었어 .. 어떤 도움을 주셔서 감사합니다!

+0

'cell = tmpCell' 행은 잠재적으로 나에게 좋지 않습니다. tmpCell은 어디에서 왔습니까? 거기에 올려 놓은 코드에는 설정되지 않습니다. UITableViewCell 표준을 사용 (또는 할당) 한 다음 UIImageView 하위 뷰를 추가하는 것이 어떨까요? –

+1

tmpCell은 nib가로드 될 때 설정되는 속성입니다. 이것은 펜촉에서 셀을로드하는 허용 된 방법입니다. –

답변

2

-tableView:cellForRowAtIndexPath:으로 [image release] 전화를 가져옵니다. 당신은 그것을 거기에 보관하지 않았기 때문에 그것을 풀어 줄 필요가 없습니다. Xcode의 정적 분석기를 사용하면 이와 같은 메모리 오류가 발생합니다.

+0

당신은 내가 얼마나 행복한 지 모르겠다. 디버깅을 도와 주셔서 감사합니다! 그렇게 단순한 실수라고 생각하지 않았습니다. – Cody

관련 문제