XMLParser 泄漏 仪器泄漏

发布于 2024-12-01 13:33:54 字数 466 浏览 1 评论 0原文

NSURL *url = [[NSURL alloc] initWithString:@"http://www.someurl.com/sample.xml"];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[url release];

XMLParser *parser = [[XMLParser alloc] initXMLParser];        //50.0%


[xmlParser setDelegate:parser];
parser = nil;
[parser release];

[xmlParser parse];                                           //50.0%
[xmlParser release];

这是我的解析代码,泄漏仪器显示泄漏。我真的不知道出了什么问题或如何解决这个问题。有什么建议吗?

NSURL *url = [[NSURL alloc] initWithString:@"http://www.someurl.com/sample.xml"];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[url release];

XMLParser *parser = [[XMLParser alloc] initXMLParser];        //50.0%


[xmlParser setDelegate:parser];
parser = nil;
[parser release];

[xmlParser parse];                                           //50.0%
[xmlParser release];

This is my parsing code and the leaks instrument is showing leaks. I really dont know what's wrong or how to fix this. Any suggestions?

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

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

发布评论

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

评论(3

Oo萌小芽oO 2024-12-08 13:33:54
parser = nil;
[parser release];

...这并不符合您的想法。假设 parser 是一个属性,那么 self.parser = nilparser = nil 会做非常不同的事情。前者将调用 parser setter 方法,该方法将释放旧值并将属性设置为 nil。后者只是将指针从当前位置更改为 nil。

通过将指针设置为 nil,您已经丢失了对该对象的引用,因此您立即泄漏了先前分配给它的对象 - 您基本上是在尝试释放一个 nil 对象。您需要删除 nil 调用,或将其放在 release 之后(见下文)。

您可能会考虑在释放它之后将指针设置为nil,以防止将来某个时候尝试访问它时出现问题。

以下是一些其他问题,可帮助提供一些背景信息:

release 与 nil -- 最佳实践

发布和发布之间的差异然后设置为无

parser = nil;
[parser release];

...this does not do what you think it does. Assuming parser is a property, then self.parser = nil and parser = nil do very different things. The former will call the parser setter method, which will release the old value and set the property to nil. The latter just changes the pointer from its current location to nil.

By setting the pointer to nil you have lost the reference to the object, so you've instantly leaked the object that was previous assigned to it - you are basically trying to release a nil object. You need to remove the nil call, or place it after the release (see below).

You may be thinking of setting a pointer to nil after you have released it, to prevent problems should you try and access it at some point in the future.

Here are a few other questions to help provide some context:

release Vs nil -- Best Practice

Difference between release and release then set to nil

丑丑阿 2024-12-08 13:33:54

我在使用 NSXMLParser 时遇到了类似的问题,但找到了一个简单的修复方法来解决内存泄漏。

我没有执行

xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

以下操作,

NSData *xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
xmlParser = [[NSXMLParser alloc] initWithData:xmlData];

而是能够摆脱内存泄漏

I have had similar issues with using NSXMLParser, but found an easy fix to resolve the memory leak.

Instead of doing

xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

Do the following

NSData *xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
xmlParser = [[NSXMLParser alloc] initWithData:xmlData];

I was able to get rid of my memory leaks

疾风者 2024-12-08 13:33:54

这里有一篇关于解析器泄漏的类似文章。我也有这个问题。这很烦人,但泄漏并不大,所以我不太担心。将查看 iOS 5 是否修复了该问题(如果确实是已知的泄漏)

编辑:我现在有兴趣看看我是否犯了上述错误!

There's a similar post here about leaks in the parser. I also have this issue. it's annoying but it's not a huge leak so I don't worry too much about it. Will see if iOS 5 fixed the problem (if it is indeed a known leak)

Edit: I'm now interested to see if I made the mistake above!

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