我在從applicationDidFinishLaunching調用的方法中有此代碼。它在模擬器中工作,但在iPhone上崩潰。在這個操作中有大約1,600個2KB的mp3文件被複制。如果我嘗試多次實例化應用程序,它最終會每次複製更多內容,直到應用程序最終會在不崩潰的情況下啓動。我釋放我分配的所有東西。我在iPhone上有大約20GB的磁盤空間。如果我逐漸註釋掉代碼並在iPhone上運行它,則copyItemAtPath似乎是可疑的。NSFileManager崩潰在應用程序代理
- (void)createCopyOfAudioFiles:(BOOL)force {
@try {
NSError *error;
NSString *component;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSEnumerator *enumerator = [[[NSBundle mainBundle]pathsForResourcesOfType:@"mp3" inDirectory:nil] objectEnumerator];
while ((component = [enumerator nextObject]) != nil) {
NSArray *temp = [component componentsSeparatedByString:@".app/"];
NSString *file = [NSString stringWithFormat:@"%@", [temp objectAtIndex:1]];
NSString *writableAudioPath = [documentsDirectory stringByAppendingPathComponent:file];
BOOL success = [fileManager fileExistsAtPath:writableAudioPath];
if (success && !force) {
continue;
} else if (success && force) {
success = [fileManager removeItemAtPath:writableAudioPath error:&error];
}
success = [fileManager copyItemAtPath:component toPath:writableAudioPath error:&error];
if (!success) {
@throw [NSException exceptionWithName:[error localizedDescription] reason:[error localizedFailureReason] userInfo:nil];
}
}
[fileManager release];
}
@catch (NSException *exception) {
NSLog(@"%@", exception);
@throw [NSException exceptionWithName:exception.name reason:exception.reason userInfo:nil];
}
@finally {
}
}
謝謝您的回覆。我同意你的觀點並實施了一個自動釋放池。結果不幸的是相同的。然後我交換了NSData writeToFile的NSFileManager copyItemAtPath。儘管如此,結果是一樣的。加載時應用程序崩潰。 – user273565 2010-04-29 02:36:04