NSXMLParser Asynch - 如何正确发布?

发布于 2024-12-05 15:29:01 字数 728 浏览 0 评论 0原文

好的。我像这样使用 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 技术交流群。

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

发布评论

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

评论(1

九公里浅绿 2024-12-12 15:29:01
myParser = [[[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]] retain];

在上面的代码中,你首先通过 alloc 为 myParser 分配内存,然后再次保留。这里,你做错了,因为只有当你拥有一个对象的所有权时,你才应该保留。但是通过 alloc 你将获得对象“myParser”的所有权”。
当你使用了该对象时,你需要释放它。
你应该这样做。

myParser = [[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]];
[myParser setDelegate:self];
[myParser performSelectorInBackground:@selector(parse) withObject:nil];
[myParser release];

同样,在委托定义中,你首先释放对象,然后将其设置为 nil。这是毫无意义的,就好像你没有任何对象的内存一样,我们如何设置任何值对此。写这样的东西..

- (void)parserDidEndDocument:(NSXMLParser *)parser  ///< The parser in question
    {
    if(_myParser)
        {
          [_myParser release];
        }
    }
myParser = [[[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]] retain];

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..

myParser = [[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]];
[myParser setDelegate:self];
[myParser performSelectorInBackground:@selector(parse) withObject:nil];
[myParser release];

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..

- (void)parserDidEndDocument:(NSXMLParser *)parser  ///< The parser in question
    {
    if(_myParser)
        {
          [_myParser release];
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文