本文介绍了ios 4.0上的NSXMLParser内存泄漏 - ** NOT ** NSCFString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的NSXMLParser完成并从内存中释放后,我得到一个奇怪的泄漏。

I'm getting a weird leak in my NSXMLParser after it's done and released from memory.

它出现了NSMapTable alloc leak。这是我的堆栈:

It comes up with NSMapTable alloc leak. Here's my stack:

   0 libSystem.B.dylib calloc
   1 libobjc.A.dylib _internal_class_createInstanceFromZone
   2 libobjc.A.dylib class_createInstance
   3 Foundation NSAllocateObject
   4 Foundation +[NSMapTable alloc] <===== this is the leak...
   5 Foundation -[NSXMLParser initWithData:]
   6 Foundation -[NSXMLParser initWithContentsOfURL:]
   7 idispatch -[RootViewController parseXML:] /Developer/iPhone  Apps/iDispatch/Classes/RootViewController.m:562 <================== this is my code calling
   8 Foundation -[NSThread main]
   9 Foundation __NSThread__main__
  10 libSystem.B.dylib _pthread_start
  11 libSystem.B.dylib thread_start

想法?

欣赏你可以流下的任何灯光!

Appreciate any light you can shed!

以下是代码:

[NSThread detachNewThreadSelector:@selector(parseXML:)
                         toTarget:self
                       withObject:requestStr];

在自己的线程上调用此方法:

which calls this method on its own thread:

- (void)parseXML:(NSString*)theURL {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:theURL]];
    DLog(@"URL: %@",theURL);
    [parser setDelegate:self];

    [parser parse];

    [parser release];

    [pool release];
    DLog(@"xml parser thread end and released");
}


推荐答案

这可能已经太晚了但是我找到了这个解决方案:

It's perhaps too late but I found this solution :

NSData * dataXml = [[NSData alloc] initWithContentsOfURL:url];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataXml];
[dataXml release];

而不是

NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

不再有内存泄漏......

No more mem leaks...

这篇关于ios 4.0上的NSXMLParser内存泄漏 - ** NOT ** NSCFString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 12:54