iPhone 上 NSXMLParser 的内存泄漏

发布于 2024-10-01 09:55:28 字数 792 浏览 0 评论 0原文

下面是我的代码,Leaks 说我在 NSMutableString alloc 方法周围出现内存泄漏。我确信这是我忽略的事情,如果有人有任何想法请告诉我。谢谢!


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if (!currentValue) {
        currentValue = [[NSMutableString alloc] initWithCapacity:[string length]];
    }

    [currentValue setString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if([elementName isEqualToString:@"phone"]){

      currentAgent.phone = currentValue;
    }

    [currentValue release];

    currentValue = nil;

}

-Agent是类初始化时创建的自定义对象。 XML 有效并且具有所有适当的开始/结束标记。

below is my code, Leaks says I am getting a memory leak around NSMutableString alloc method. I am sure it is something I simply overlooked, let me know if anyone has any thoughts. Thanks!


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if (!currentValue) {
        currentValue = [[NSMutableString alloc] initWithCapacity:[string length]];
    }

    [currentValue setString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if([elementName isEqualToString:@"phone"]){

      currentAgent.phone = currentValue;
    }

    [currentValue release];

    currentValue = nil;

}

-Agent is a custom object that was created when the class was initialized. The XML is valid and has all the appropriate begin/end tags.

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

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

发布评论

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

评论(1

夏九 2024-10-08 09:55:28

查看这段代码,我认为您的 Agent 类更有可能泄漏电话。假设代理使用 retain 作为电话属性,这将导致电话持续的时间比应有的时间长。

即使额外的保留在其他地方,对象的创建者也会因泄漏而“归功于”。

换句话说,在 Agent 中:

- (void)dealloc {
    self.phone = nil;
    // anything else you need to do
    [super dealloc];
}

Looking over this code, I think it's more likely that your Agent class is leaking phone. Assuming Agent uses retain for the phone property, this will cause the phone to persist longer than it should.

The creator of the object gets "credited" with the leak, even if the extra retain is somewhere else.

In other words, in Agent:

- (void)dealloc {
    self.phone = nil;
    // anything else you need to do
    [super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文