NSXMLParser Asynch - 如何正确发布?
好的。我像这样使用 NSXMLParser:
myParser = [[[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]] retain];
[myParser setDelegate:self];
[myParser performSelectorInBackground:@selector(parse) withObject:nil];
我有自己的子类,以便执行诸如拥有内存池和一些实例数据之类的操作。基本上,可以将 BMLT_Parser 视为与 NSXMLParser 相同。
请注意,我是异步调用它的,因此同步调用后没有简单的释放。
我所做的是声明以下委托函数:
- (void)parserDidEndDocument:(NSXMLParser *)parser ///< The parser in question
{
[myParser release];
myParser = nil;
}
myParser 是解析器的实例变量。基本上,回调中的 myParser == parser 。
现在,问题是 Instruments 告诉我解析器正在泄漏。解析器泄漏严重,因为它们打包了很多行李。
我还应该如何解除分配异步解析器?我强烈怀疑我只需要被定向到“M”,这样我就可以“RTFM”。
谢谢!
OK. I'm using NSXMLParser like so:
myParser = [[[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]] retain];
[myParser setDelegate:self];
[myParser performSelectorInBackground:@selector(parse) withObject:nil];
I have my own subclass in order to do things like have a memory pool and some instance data. Basically, it's OK to think of BMLT_Parser as the same as NSXMLParser.
Note that I am calling it asynchronously, so there's no simple deallocation after a synchronous call.
What I have done, is declare the following delegate function:
- (void)parserDidEndDocument:(NSXMLParser *)parser ///< The parser in question
{
[myParser release];
myParser = nil;
}
myParser is an instance variable of the parser. Basically, myParser == parser in the callback.
Now, the problem is that Instruments tells me that the parser is leaking. Parsers leak badly, because they pack a lot of luggage.
How else should I dealloc asych parsers? I strongly suspect that I simply need to be directed to an "M", so that I can "RTFM".
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在上面的代码中,你首先通过 alloc 为 myParser 分配内存,然后再次保留。这里,你做错了,因为只有当你拥有一个对象的所有权时,你才应该保留。但是通过 alloc 你将获得对象“myParser”的所有权”。
当你使用了该对象时,你需要释放它。
你应该这样做。
同样,在委托定义中,你首先释放对象,然后将其设置为 nil。这是毫无意义的,就好像你没有任何对象的内存一样,我们如何设置任何值对此。写这样的东西..
In the above code, u r firstly allocating memory for myParser by alloc, and again u r retaining .Here, u r doing wrong as u should retain only when u have take ownership of an object.But through alloc u will get ownership of the object"myParser".
And when u have used the object, u need to release that.
you should do like something this..
Again, in the delegate definition, you r firstly releasing the object then setting that to nil.This is quite meaningless ,as if u don't have memory for any object, how can we set any value to that. write something like this..