Iphone NSFileMananaer和x27上的泄漏;s目录路径的内容

Iphone NSFileMananaer和x27上的泄漏;s目录路径的内容,iphone,objective-c,memory,memory-leaks,Iphone,Objective C,Memory,Memory Leaks,在“checkFiles”方法中,我只是搜索了一个NSDocumentDirectory路径并请求文件列表 - (void) checkFiles{ NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [pathList objectAtIndex:0]; NSAr

在“checkFiles”方法中,我只是搜索了一个NSDocumentDirectory路径并请求文件列表

- (void) checkFiles{
        NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [pathList objectAtIndex:0];

        NSArray *dirArray = [myFileManager contentsOfDirectoryAtPath:path error:NULL]; // leak occurs here!

        /* .... */
}
-

-

泄漏仪器检测NSFileManager的目录路径方法的内容泄漏

这是我在Instruments中单击名为NSCFString的泄漏对象时的堆栈跟踪

   0 CoreFoundation __CFAllocatorSystemAllocate
   1 CoreFoundation CFAllocatorAllocate
   2 CoreFoundation _CFRuntimeCreateInstance
   3 CoreFoundation __CFStringCreateImmutableFunnel3
   4 CoreFoundation CFStringCreateWithBytes
   5 Foundation -[NSFileManager directoryContentsAtPath:matchingExtension:options:keepExtension:error:]
   6 Foundation -[NSFileManager contentsOfDirectoryAtPath:error:]
因为我经常调用“checkFiles”方法来更新包含文件列表的UITableView,所以无法避免此泄漏

我应该如何修复这种系统泄漏


谢谢。

您确定dirArray没有保留在某个地方吗?可能是伪装的,比如当作为参数传递给另一个方法时


如果您想进行半自动检查,请使用Instruments的泄漏工具检查您发送的所有保留/释放消息(直接或间接),并逐一检查。

谢谢您的回答!实际泄漏发生在不同的地方,但它们共享相同的字符串内存地址。我做了[myArray addObject:[dirArray objectAtIndex:1]复制]]很高兴听到您发现它很有用。