Objective-C 发布

发布于 2024-09-28 08:47:24 字数 1158 浏览 7 评论 0原文

我在 DNS.h 中有这个类

@interface DNS : NSObject <NSXMLParserDelegate, NSNetServiceDelegate> {
     NSMutableArray *servsPublished;
}
@property(nonatomic, retain) NSMutableArray *servsPublished;

,然后在实现文件 DNS.m 中有一个方法,我可以释放它,然后得到 exec bad access memory 错误。这是方法,它符合 NSXMLParserDelegate 协议

-(void) parserDidEndDocument:(NSXMLParser *)parser {
    NSNetService *client;
    for (NSDictionary *aService in servToPublish) {
        client = [[NSNetService alloc] initWithDomain:@"local" 
                                             type:[aService objectForKey:@"serviceType"] 
                                             name:[aService objectForKey:@"name"] 
                                             port: [(NSNumber *)[aService objectForKey:@"port"] intValue]];

        [client setDelegate: self];
        [client publish];

        //release this service and the client
       [aService release];
       //[client release];
   }

   //free the array of Dictionary containing the services
   [servToPublish release];
}

也许第一个想法是 nil,但实际上我在方法中使用它检查是否为 nil,然后释放它。

和保留财产有关系吗? 谢谢。

I have this class in DNS.h

@interface DNS : NSObject <NSXMLParserDelegate, NSNetServiceDelegate> {
     NSMutableArray *servsPublished;
}
@property(nonatomic, retain) NSMutableArray *servsPublished;

Then in the implementation file DNS.m there's a method where I release it and I get the exec bad access memory error. This is method, it conforms to the NSXMLParserDelegate protocol

-(void) parserDidEndDocument:(NSXMLParser *)parser {
    NSNetService *client;
    for (NSDictionary *aService in servToPublish) {
        client = [[NSNetService alloc] initWithDomain:@"local" 
                                             type:[aService objectForKey:@"serviceType"] 
                                             name:[aService objectForKey:@"name"] 
                                             port: [(NSNumber *)[aService objectForKey:@"port"] intValue]];

        [client setDelegate: self];
        [client publish];

        //release this service and the client
       [aService release];
       //[client release];
   }

   //free the array of Dictionary containing the services
   [servToPublish release];
}

Maybe the first thought is that is nil, but actually I use it inside the method checking whether is nil or not, and then free it.

Does It have something to do with the retain property?
THX.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

羅雙樹 2024-10-05 08:47:24

不要在 for 循环中释放 aService。您尚未在此处创建它们(没有分配/新建/复制),因此不应释放它们。当您发布 servToPublish 时,所有内容都会自动发布,但如果您在循环中发布它们,则会导致发送太多发布消息。

在这种情况下,在分配客户端时释放它是正确的。

Do NOT release aService in the for loop. You have not created them here (no alloc/new/copy) so you should not release them. When you release servToPublish all content will automatically be released, but if you release them in your loop you will cause too many release messages to be sent.

It's correct to release client in this case as you do allocate it.

夜访吸血鬼 2024-10-05 08:47:24

首先,运行构建并分析。静态分析器非常擅长指出某些类型的内存管理问题。

其次,除非您处于 dealloc 状态,否则您可能不想直接在 servsPublished 上调用release。相反,请使用 self.servsPublished = nil 。这将正确释放该对象并将相应的 ivar 设置为 nil。

First, run build and analyze. The static analyzer is pretty good at pointing out certain types of memory management problems.

Second, unless you're in dealloc, you probably don't want to call release directly on servsPublished. Instead, use self.servsPublished = nil. That will properly release the object and set the corresponding ivar to nil.

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