NSString 发布时崩溃

发布于 2024-11-19 11:22:44 字数 1288 浏览 1 评论 0原文

这是我的 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]:消息发送到已释放的实例 0x62255700x6225570h.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2024-11-26 11:22:44

您需要使用 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.

流殇 2024-11-26 11:22:44

在您的 initWithItem: 中,您需要执行 item = [someItem copy]; 您也可以执行 item = [someItem keep] ;,但如果您的字符串是 NSMutableString,则会导致问题。

崩溃的原因是您传递了一个自动释放的字符串,并且您的 initWithItem: 没有说“我需要这个字符串保留在周围”(保留)或 >“我需要这个字符串的个人版本”(副本)。因此,当您在释放中释放字符串时,该字符串会被频繁释放。

根据您的消息来源,我愿意打赌崩溃不会发生在您发布的代码中,而是实际上发生在 NSAutoreleasePool 最终释放字符串时。

In your initWithItem:, you need to do item = [someItem copy]; You could also do item = [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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文