NSString 发布时崩溃
这是我的 ItemInfo
类接口
@interface ItemInfo : NSObject {
NSString *item;
}
@property (nonatomic, copy) NSString *ipaddress;
...以及我从其他地方使用此类的实现
@synthesize item;
- (id) initWithItem:(NSString *)someItem {
self = [super init];
if(self) {
item = someItem; // Ideally these things should happen here.
// Since item is a NSString and not NSMutableString,
// it should be sent a retain, thus
// making its refcount = 1
// Is my understanding correct?
}
return self;
}
- (void) dealloc {
[item release]; // I own 'item', so I should release it when done
[super dealloc];
}
,如下所示:
char *str = inet_ntoa(addy->sin_addr);
ItemInfo *h = [[ItemInfo alloc] initWithItem:[NSString stringWithFormat:@"%s", str]];
ContentBrowserViewController *d
= [[ContentBrowserViewController alloc] initWithItemInfo:h];
[self.navigationController pushViewController:d animated:YES];
[h release];
[d release];
我遇到的崩溃是 *** -[CFString release]:消息发送到已释放的实例 0x6225570
。 0x6225570
是h.item
的地址
我哪里出错了?
Here is my ItemInfo
class interface
@interface ItemInfo : NSObject {
NSString *item;
}
@property (nonatomic, copy) NSString *ipaddress;
... and the implementation
@synthesize item;
- (id) initWithItem:(NSString *)someItem {
self = [super init];
if(self) {
item = someItem; // Ideally these things should happen here.
// Since item is a NSString and not NSMutableString,
// it should be sent a retain, thus
// making its refcount = 1
// Is my understanding correct?
}
return self;
}
- (void) dealloc {
[item release]; // I own 'item', so I should release it when done
[super dealloc];
}
I am using this class from elsewhere like thus:
char *str = inet_ntoa(addy->sin_addr);
ItemInfo *h = [[ItemInfo alloc] initWithItem:[NSString stringWithFormat:@"%s", str]];
ContentBrowserViewController *d
= [[ContentBrowserViewController alloc] initWithItemInfo:h];
[self.navigationController pushViewController:d animated:YES];
[h release];
[d release];
The crash I am encountering is*** -[CFString release]: message sent to deallocated instance 0x6225570
. 0x6225570
is the address of h.item
Where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 self.item = someItem 来调用 setter。您当前忽略设置器,因此不复制/拥有该字符串。
You need to call your setter using
self.item = someItem
. You currently disregard the setter and therefore do not copy/own the String.在您的
initWithItem:
中,您需要执行item = [someItem copy];
您也可以执行item = [someItem keep] ;
,但如果您的字符串是 NSMutableString,则会导致问题。崩溃的原因是您传递了一个自动释放的字符串,并且您的
initWithItem:
没有说“我需要这个字符串保留在周围”(保留)或 >“我需要这个字符串的个人版本”(副本)。因此,当您在释放中释放字符串时,该字符串会被频繁释放。根据您的消息来源,我愿意打赌崩溃不会发生在您发布的代码中,而是实际上发生在 NSAutoreleasePool 最终释放字符串时。
In your
initWithItem:
, you need to doitem = [someItem copy];
You could also doitem = [someItem retain];
, but that would cause problems if your string was a NSMutableString.The reason for the crash is that you pass an autoreleased string, and your
initWithItem:
doesn't say "I need this string to stay around" (retain) or "I need my personal version of this string" (copy). The string thus gets released too often as you do release it in your dealloc.According to your source, I'm willing to bet that the crash does not happen in the code you've posted but actually when the NSAutoreleasePool is finally releasing the string.