uiimagepickerview controller creating memory leaks in iphone - why?

前端 未结 4 719
小鲜肉
小鲜肉 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.

    0 讨论(0)
  • 2021-01-07 11:36

    I can explain this because I was having the same problem.

    Don't test memory on the simulator! If you test the apple code on a device the memory problem disappears.

    I was having a memory alloc leak which I found in Instruments. All I was doing was opening and closing the image picker (open/cancel) and using Apple code, my code and other people's code, just like yours above.

    All were showing the allocation going up and up each time, as if the picker was not being released. If you tried to release it, it would crash (over released).

    Then I found a really helpful web page which basically stated:

    "This doesn't happen when testing on the device"

    So I switched from the simulator and ran the tests on the device. Lo & behold there was no allocation increase and it behaved normally.

    This however is totally evil and now we can place no trust in the simulator to do a reliable job.

    I want to add this to save people, the time, pain and bewilderment of wondering wtf is going on!

    0 讨论(0)
  • 2021-01-07 11:42

    UIImagePickerController loads and initializes PhotoLibrary.framework the first time it is shown. This memory won't be reclaimed until your application is closed.

    (the code you posted doesn't appear to have leaks as-is, but that doesn't mean it won't interact with the rest of your application in a way that causes them)

    0 讨论(0)
  • 2021-01-07 11:54

    I dont know about the rest of the code, but do you ever have a release?

    [imagePickerController release]
    
    0 讨论(0)
提交回复
热议问题