我在从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