2011-05-03 78 views
0

將自定義對象添加到目標c中的mutableArray時發生內存SIGBUS錯誤。發佈自定義對象時出現Iphone SIGBUS錯誤

@interface stak : NSObject { 
    NSString *idval, 
    *username, 
    *userid, 
    *password, 
    *snippet, 
    *curStakId, 
    *pageCount, 
    *memberCount, 
    *imgURL, 
    *tags; 
    UIImage *icon; 
} 

@property (nonatomic,retain) NSString *idval,*username,*userid,*password,*curStakId,*snippet,*pageCount,*memberCount,*imgURL,*tags; 
@property (nonatomic,retain) UIImage *icon; 

-(id)initWithidval:(NSString *)idvalue 
      username:(NSString *)user 
      userid:(NSString *)uid 
      password:(NSString *)pass 
     curStakId:(NSString *)stakid 
      snippet:(NSString *)snip 
     pageCount:(NSString *)page 
     memberCount:(NSString *)members 
       tags:(NSString *)tag_vals 
      imgURL:(NSString *)img 
       icon:(UIImage *)iconImg; 



@end 

與.m

@implementation stak 
@synthesize idval; 
@synthesize username; 
@synthesize userid; 
@synthesize password; 
@synthesize curStakId; 
@synthesize snippet; 
@synthesize pageCount; 
@synthesize memberCount; 
@synthesize imgURL; 
@synthesize icon; 
@synthesize tags; 

-(id)initWithidval:(NSString *)idvalue 
      username:(NSString *)u 
      userid:(NSString *)uid 
      password:(NSString *)p 
     curStakId:(NSString *)stakid 
      snippet:(NSString *)snip 
     pageCount:(NSString *)page 
     memberCount:(NSString *)members 
       tags:(NSString *)tag_vals 
      imgURL:(NSString *)img 
       icon:(UIImage *)iconImg{ 

    if (self = [super init]) { 
     [self setIdval:idvalue]; 
     [self setUsername:u]; 
     [self setUserid:uid]; 
     [self setPassword:p]; 
     [self setCurStakId:stakid]; 
     [self setSnippet:snip]; 
     [self setPageCount:page]; 
     [self setMemberCount:members]; 
     [self setTags:tag_vals]; 
     [self setImgURL:img]; 
     [self setIcon:iconImg]; 
    } 
    return self; 
} 

-(void)dealloc{ 

    [idval release]; 
    [username release]; 
    [userid release]; 
    [snippet release]; 
    [imgURL release]; 
    [icon release]; 
    [tags release]; 
    [curStakId release]; 
    [memberCount release]; 
    [password release]; 
    [super dealloc]; 
} 

@end 

而這正是它被稱爲和釋放。

NSMutableArray *_return_staks = [[NSMutableArray alloc]init];  

stak *_stakItem = [[stak alloc]initWithidval:[NSString stringWithFormat:@"%@",[staks objectAtIndex:i]] 
               username:[NSString stringWithFormat:@"%@",[creators objectAtIndex:i]] 
                userid:[NSString stringWithFormat:@"%@",[creatorid objectAtIndex:i]] 
               password:[NSString stringWithFormat:@"%@",[privacy objectAtIndex:i]] 
               curStakId:[NSString stringWithFormat:@"%@",[idvals objectAtIndex:i]] 
               snippet:tempString 
               pageCount:tempPcount 
              memberCount:tempMcount 
                tags:[NSString stringWithFormat:@"%@",[tags objectAtIndex:i]] 
                imgURL:[NSString stringWithFormat:@"%@",[img objectAtIndex:i]] 
                icon:nil]; 

      [_return_staks addObject:_stakItem]; 

      [_stakItem release]; 

當我去引用存儲的項目我得到一個SIGBUS錯誤,但是當我刪除「[_stakItem發行]」它工作正常,但是這造成泄漏。有什麼方法可以糾正這個問題嗎?

回答

2

如果沒有看到實際崩潰的上下文,很難給出明確的答案,但是您可能在某處放出_stackItem。這可能是由引用它引起的,但是釋放數組是唯一擁有它的東西。實際上你發佈的代碼沒有問題(當然,你的字符串屬性應該是複製屬性,但這不是導致你崩潰的原因)。

發佈後是否使用了_stakItem?

你有這樣一個順序:

stak* foo = [_return_staks objectAtIndex: fooIndex]; 

// etc 

[_return_staks release]; 

// etc 

[foo doSomething]; // Likely to have gone away by now. 
相關問題