pnmn
pnmn

Reputation: 1117

Leak on NSFileMananer's contentsOfDirectoryAtPath

In "checkFiles" method, I just searched a NSDocumentDirectory path and requested the list of files.

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

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

        /* .... */
}

-

-

Leaks Instruments detect leaks on NSFileManager's contentsOfDirectoryAtPath method.

This is the stack trace when I clicked a leaked object named NSCFString in Instruments.

   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:]

Because I call "checkFiles" method very often to update my UITableView containing a file list, I can't avoid this leak.

How should I fix this kind of system leak?

Thank you.

Upvotes: 1

Views: 943

Answers (1)

Steven Kramer
Steven Kramer

Reputation: 8503

And you are sure the dirArray is not retained somewhere? Perhaps in disguise, such as when passed as an argument to another method?

If you want to do a half-automated check, use Instruments' Leaks tool to check for all the retain/release messages you are sending (directly or indirectly) and check each one.

Upvotes: 2

Related Questions