uiimagepickerview controller creating memory leaks in iphone - why?

你。 提交于 2019-12-01 01:24:35

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.

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

[imagePickerController release]

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)

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!