gpt4 book ai didi

释放自定义对象时出现 Iphone SIGBUS 错误

转载 作者:行者123 更新时间:2023-11-28 23:16:59 25 4
gpt4 key购买 nike

在 objective-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]”时它工作正常,但这会造成泄漏。有什么办法可以纠正这个问题吗?

最佳答案

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

您是否在发布后使​​用 _stakItem?

你有这样的序列吗:

 stak* foo = [_return_staks objectAtIndex: fooIndex];

// etc

[_return_staks release];

// etc

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

关于释放自定义对象时出现 Iphone SIGBUS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5870298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com