2012-10-01 44 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中生成實際泄漏比在託管內存代碼中要困難得多。真正做到這一點的唯一方法是一個孤立的強循環。你需要用更多的信息/細節來編輯你的問題。 –