方法返回对象时 iPhone 内存泄漏
我正在 iOS 4 上进行开发,我的 appDelegate 中有这个方法。它是从几个表视图数据源委托调用的。仪器给了我这些(在设备和模拟器上) Malloc 512 字节、Malloc 512 字节、NSConcreteMapTable(基础)。 Malloc 没有显示任何负责任的库。
这是返回对象的方法:
- (NSXMLParser *) getXmlParserFrom:(NSString *)remoteFile andCacheToFile:(NSString *) fileName forceRefresh:(BOOL) doRefresh {
NSXMLParser *xmlParser;
//FIRST TRY TO LOAD THE XML FROM CACHED FILE
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *xmlDocumentFromCache = [[NSString alloc] initWithContentsOfFile:filePath];
if ( xmlDocumentFromCache && !doRefresh ) {
NSData *xmlData = [NSData dataWithContentsOfFile:filePath];
xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
} else {
NSURL *xmlFileURL = [NSURL URLWithString:remoteFile];
NSString *contentsOfRemoteFile = [NSString stringWithContentsOfURL:xmlFileURL];
//CACHE THE FILE
BOOL cacheResult = [contentsOfRemoteFile writeToFile:filePath atomically:YES];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFileURL];
}
[xmlDocumentFromCache release];
return [xmlParser autorelease];
}
I'm developping on iOS 4 and I've got this method in my appDelegate. It is called from a couple of tableviews datasource delegates. Instruments gave me these (on device and simulator)
Malloc 512 bytes, Malloc 512 bytes, NSConcreteMapTable (Foundation). The Mallocs don't show any responsible library.
Here's the method returning the object:
- (NSXMLParser *) getXmlParserFrom:(NSString *)remoteFile andCacheToFile:(NSString *) fileName forceRefresh:(BOOL) doRefresh {
NSXMLParser *xmlParser;
//FIRST TRY TO LOAD THE XML FROM CACHED FILE
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *xmlDocumentFromCache = [[NSString alloc] initWithContentsOfFile:filePath];
if ( xmlDocumentFromCache && !doRefresh ) {
NSData *xmlData = [NSData dataWithContentsOfFile:filePath];
xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
} else {
NSURL *xmlFileURL = [NSURL URLWithString:remoteFile];
NSString *contentsOfRemoteFile = [NSString stringWithContentsOfURL:xmlFileURL];
//CACHE THE FILE
BOOL cacheResult = [contentsOfRemoteFile writeToFile:filePath atomically:YES];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFileURL];
}
[xmlDocumentFromCache release];
return [xmlParser autorelease];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚使用
这是一个解决方法,但它确实有效。
另一方面,我发现如果您总是在设备上而不是模拟器上运行 Instruments,它可以在 Lion/Xcode 4.1 中可靠地工作。在模拟器上,这个过程似乎花费了很长的时间。
I just fixed this by using the method outlined in this post.
It's a workaround, but it works.
On another note, I have found that Instruments works reliably in Lion/Xcode 4.1 if you always run it on the device, as opposed to the simulator. On the simulator, it seems to have a devil of a time attaching to the process.