uiimagepickerview controller creating memory leaks in iphone - why?

前端 未结 4 765
小鲜肉
小鲜肉 2021-01-07 11:18

uiimagepickerview controller creating memory leaks in iphone - why?

Try to implement ui image picker view controller in your application & debug it. You will fin

4条回答
  •  滥情空心
    2021-01-07 11:34

    Even though you have the nil check, it's still possible to leak memory. I think what is happening here is that you are calling alloc / init multiple times, but only releasing once. My guess it that addPhoto: is wired up to some button click, dealloc would only be called once when the delegate is trying to destroy. This creates a situation like this:

    • button click
      • alloc / init
    • button click
      • alloc / init (memory leak on first alloc'd picker)
    • close window
      • dealloc (free second alloc'd picker)

    A better way might be the way Apple does it in the PhotoLocations and iPhoneCoreDataRecipes examples:

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    [self presentModalViewController:imagePicker animated:YES];
    [imagePicker release];
    

    Then listen for the didFinishPickingImage and imagePickerControllerDidCancel messages to your delegate and a call to [self dismissModalViewControllerAnimated:YES]; in both places should suffice.

提交回复
热议问题