iPhone代码泄漏,我不知道为什么

时间:2009-06-24 15:53:27

标签: iphone cocoa-touch memory-management memory-leaks

这是一个众所周知的片段,如何从iPhone照片库中选择一张图片:

- (IBAction)selectExistingPicture {
  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentModalViewController:picker animated:YES];
            [picker release];

  }
}

您可以在此处看到乐器(fullscreen)的屏幕截图。

alt text http://img.skitch.com/20090624-rtqp2mgsnyynkgb97c9e8d2g9c.jpg

为什么泄漏?我不明白,因为我认为选择器被正确释放了。

3 个答案:

答案 0 :(得分:6)

UIImagePickerControllerknown to leak。如果您打算多次使用它,建议您重复使用单个实例

答案 1 :(得分:4)

当你离开方法时,你正在呈现选择器但是丢失了指向它的指针。因为它是分配内存,那就是你的泄漏。尝试:

UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];

答案 2 :(得分:2)

你不应该在UIImagePickerController上做自动释放吗?

UIImagePickerController * picker = [[[UIImagePickerController alloc] init] autorelease];