开发者

Iphone SIGBUS error with releasing custom object

getting a memory SIGBUS error when adding a custom object to a mutableArray in objective c.

@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

and the .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

and this is where it is called and released.

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];

When i go to reference the stored item i get a SIGBUS error, however when i remove the "[_stakItem release]" it works fine, however this creates a leak. Is there any way to correct this?


It's difficult to give you a definitive answer without seeing the context of the actual crash, but you are probably over releasing _stackItem somewhere. This is probably caused by keeping a reference to it but releasing the array which is the only thing that owns it. There is actually nothing wrong with the code you have posted (well, your string properties should really be copy properties, but that is not what is causing your crash).

Are you using _stakItem after the release?

Do you have a sequence like:

 stak* foo = [_return_staks objectAtIndex: fooIndex];

 //  etc 

[_return_staks release];

// etc

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜