Stefan
Stefan

Reputation: 29017

iPhone code leaks and I don't know why

this is a well known snippet, how to select a picture from the iPhone photo library:

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

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

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

  }
}

Here you can see a screenshot of Instruments (fullscreen).

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

Why does it leak? I don't understand it, because picker is released properly, I think.

Upvotes: 1

Views: 433

Answers (3)

rpetrich
rpetrich

Reputation: 32316

The UIImagePickerController is known to leak. If you are going to use it more than once it's recommended that you reuse a single instance

Upvotes: 6

mharper
mharper

Reputation: 3272

Shouldn't you be doing an autorelease on the UIImagePickerController?

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

Upvotes: 2

tmh
tmh

Reputation: 672

You are presenting picker but then losing the pointer to it when you leave the method. As it is allocated memory, that is your leak. Try:

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

Upvotes: 4

Related Questions