iphoneuipickerviewcontroller

uipickerview causes memory leak


My problem is there is a memory leak in my application even though I am doing everything right. I am allocing a local uipickerviw, assign that to class member (pickerview) then I dealloc the local uipickerview. Still I get a memory leak, and I don't understand why.

UIImagePickerController *the_pImagePicker=[[UIImagePickerController alloc] init];
//memory leak is displayed on this line.
self.m_pImagePicker = the_pImagePicker;
self.m_pImagePicker.delegate = self;    
[the_pImagePicker release];

Solution

  • There should not be any leaks after the very first creation/assignment of the picker.

    First time:

    After the first line the retain count of the_pImagePicker is 1. After the second line it becomes 2 because m_pImagePicker is a "retain" property. After the last line it goes down to 1 again.

    However if m_pImagePicker is defined as "retain" property, and if you call this piece of code again and do not release the self.m_pImagePicker before that, you will leak memory:

    Second time:

    On the second line you re-assign the self.m_pImagePicker pointer, so the object referenced by self.m_pImagePicker after "First time" will be dumped with retain counter still equal to 1 == leak.

    I would initially set self.m_pImagePicker to nil, and before execution of your code would check if it is still nil. If it is not, I would release it, set it to nil (just to be consistent with the "nil" logic) and then would execute new assignment.