问题描述
我是iPhone编程的新手,正在针对Instruments中的Leaks工具运行我的应用程序。该工具发现了一些泄漏,所有这些泄漏似乎都指向使用NSXMLParser的类中的一行代码:
I am new to iPhone programming, and am running my app against the Leaks tool in Instruments. The tool is finding a few leaks, all of which seem to point to 1 line of code in a class which uses NSXMLParser:
- (BOOL)parse{
NSURL *url = [[NSURL alloc] initWithString:@"[url here]"];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
NSLog(@"NSXMLParser initialized");
[parser parse];
[url release];
[parser release];
return YES;
}
该工具指向创建解析器的行存在泄漏:
The tool points to the line creating the parser as having the leak:
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
有人能指出我在这个方向上的正确方向吗?我已经在代码中进行引用计数一个小时了,没有运气。
Can anyone point me in the right direction on this one? I've been going through reference counts in my code for an hour now with no luck.
更新:
好吧,从2个答案中获取建议,我在创建NSURL之前添加了以下几行:
Ok, taking the suggestions from 2 answers, I added these lines before creating the NSURL:
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
我在释放解析器之前添加了这一行:
And I added this line right before releasing the parser:
[parser setDelegate:nil];
每次添加都减少了泄漏次数,现在我降到了2。CFNetwork得1分还有一个指向基金会作为负责图书馆。检查两者上的调用堆栈根本不会显示我的任何代码。
Each addition reduced the number of leaks, and now I'm down to 2. One points to CFNetwork and one points to Foundation as the responsible library. Examining the call stack on both doesn't show any of my code at all.
这里还有什么我可能做错了吗?
Is there anything else I might be doing wrong here?
推荐答案
我在另一个线程上发现了此问题:
I found on this on another thread:
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
(来源:)
但是我还没有不要测试此解决方案。
But I haven't test this solution.
这篇关于在此Objective-C NSXMLParser代码中找不到泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!