发布自定义对象时出现Iphone SIGBUS错误

时间:2011-05-03 13:36:53

标签: iphone objective-c ios

在目标c中向mutableArray添加自定义对象时出现内存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 release]”时它工作正常,但这会产生泄漏。有没有办法纠正这个?

1 个答案:

答案 0 :(得分:2)

如果没有看到实际崩溃的上下文,很难给出明确的答案,但是你可能已经过度释放_stackItem了。这可能是因为保持对它的引用,但释放了唯一拥有它的数组。您发布的代码实际上没有任何问题(嗯,您的字符串属性应该是复制属性,但这不是导致崩溃的原因)。

你在发布后使​​用_stakItem吗?

你有一个类似的序列:

 stak* foo = [_return_staks objectAtIndex: fooIndex];

 //  etc 

[_return_staks release];

// etc

[foo doSomething]; // Likely to have gone away by now.