title: How can I avoid EXC_BAD_ACCESS when checking if directories exist?
body: <p>I'm building an app which caches images in the Documents directory of the app bundle. In order to make sure the directories exist, I want to check to see if they exist and, if they don't, create them at the point at which the application starts.</p>
  
  <p>Currently, I'm doing this in <code>didFinishLaunchingWithOptions:</code> like so:</p>
  
  <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
      NSArray *directoriesToCreate = [[NSArray alloc] initWithObjects:
                                      @"DirA/DirA1",
                                      @"DirA/DirA2",
                                      @"DirB/DirB2",
                                      @"DirB/DirB2",
                                      @"DirC",
                                      nil];
  
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsPath = [paths objectAtIndex:0];
  
      for (NSString *directoryToCreate in directoriesToCreate) {
  
          NSString *directoryPath = [documentsPath stringByAppendingPathComponent:directoryToCreate];
          NSLog(directoryPath);
          if (![[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:YES]) {
  
              NSError *directoryCreateError = nil;
              [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
                                        withIntermediateDirectories:YES
                                                         attributes:nil
                                                              error:&amp;directoryCreateError];
  
  
          }
  
      }
  
      [window addSubview:navigationController.view];
      [window makeKeyAndVisible];
  
      return YES;
  
  }
  </code></pre>
  
  <p>On the very first run of the application – when none of the directories exist – the application runs, the directories are created as expected and everything runs just fine.</p>
  
  <p>When the application is terminated, and runs again, I get an EXC_BAD_ACCESS signal on the <code>fileExistsAtPath:</code> call on <code>[NSFileManager defaultManager]</code>.</p>
  
  <p>What I don't understand is why this runs just fine when the directories don't exist, but it falls over when they do exist.</p>
  
  <p>Can anyone offer any assistance?</p>