我在从 applicationDidFinishLaunching 调用的方法中有此代码。它在模拟器中工作,但在 iPhone 上崩溃。在此操作中复制了大约 1,600 个 2KB 的 mp3 文件。如果我尝试多次实例化应用程序,它最终会每次复制更多,直到应用程序最终启动而不会崩溃。我正在释放我分配的一切。我在 iPhone 上有大约 20GB 的可用磁盘空间。如果我逐步注释掉代码并在 iPhone 上运行它,copyItemAtPath 似乎是嫌疑犯。
- (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 {
}
}