iPhone临时文件写入中的竞争条件(?)

iPhone临时文件写入中的竞争条件(?),iphone,objective-c,file-io,race-condition,Iphone,Objective C,File Io,Race Condition,我正在iPad模拟器中创建一些临时文件。为了测试文件创建,我创建了文件,然后将其读回。下面是一些代码来说明这一点: -(NSString *) writeToTempFile:(UIImage*) image{ NSString *path = [self createTemporaryFile]; NSLog(@"path: %@", path); NSData *data = UIImageJPEGRepresentation(image, 1); [data writeToFile:pat

我正在iPad模拟器中创建一些临时文件。为了测试文件创建,我创建了文件,然后将其读回。下面是一些代码来说明这一点:

-(NSString *) writeToTempFile:(UIImage*) image{
NSString *path = [self createTemporaryFile];
NSLog(@"path: %@", path);
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES]; 
free(data);
return path;
}

-(UIImage *) readTempFile:(NSString *) path{
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:data];
return image;
}
在最后一个函数将UIImage写入相册之前,我一个接一个地调用这些方法

UIImageWriteToSavedPhotosAlbum(image2, self, nil, nil);
问题是,这总是会在我的应用程序第三次执行时崩溃。第一次和第二次,它成功地完成了所有这些并存储到相册中。第三次撞到家里。有什么想法吗

NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES]; 
free(data);
从UIImageJPEG表示返回的NSData是
-autorelease
d。没有必要
free()
it。如果
free()
任何Objective-C对象-发送
-release
消息,则是错误的

请通读这本书

从UIImageJPEG表示返回的NSData是
-autorelease
d。没有必要
free()
it。如果
free()
任何Objective-C对象-发送
-release
消息,则是错误的

请通读这本书