2012-10-01 87 views
-1

我加载了很多来自plist的注释,所有加载都很好,但是如果我从NSCachesDirectory加载内存泄漏工具显示泄漏。如果我从url加载,没有泄漏。我在项目中使用ARC。如果从NSCachesDirectory加载内存泄漏

内存泄露

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
     NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"test.plist"]; 
     NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; // leaking here 

无漏

NSString *urlStr = [[NSString alloc] 
        initWithFormat:@"http://www.domain.com/test.plist" ]; 

NSURL *url = [NSURL URLWithString:urlStr]; 
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url]; 
+0

其实,我现在看到你已经问这个问题,那个人实际上有更多的数据。请不要只是重新提出一个问题,尤其是在背景/信息少得多的情况下。 –

+0

可能重复[内存泄漏在新线程使用弧](http://stackoverflow.com/questions/12653016/memory-leak-on-new-thread-using-arc) –

回答

0

我不知道为什么仪器会显示一个,而不是其他的泄漏,但它几乎肯定由于某些问题,这不是由这些代码片段代表。您可以验证通过简单地使用URL方法(这是建议的路要走,无论如何,使用文件URL在新的代码路径喜欢)来查找文件:

NSError* error = nil; 
NSURL* fileURL = [[[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error] URLByAppendingPathComponent:@"test.plist"]; 
if(!fileURL) { /* deal with error */ } 
// If this still leaks, it's due to the way your code is structured and 
// you will have to provide more details. 
NSDictionary* dict = [[NSDictionary alloc] initWithContentsOfURL:fileURL]; 
+0

仍然泄漏与您的代码示例 –

+0

@PavelKaljunen这意味着它是你的代码中的其他东西,你需要发布更多的上下文。在ARC中生成实际泄漏比在托管内存代码中要困难得多。真正做到这一点的唯一方法是一个孤立的强循环。你需要用更多的信息/细节来编辑你的问题。 –