views:

50

answers:

1

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. i dnt understand y. somebody plz help me.

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];
A: 

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.

spbfox
Are you sure the dealloc gets ever called? It would be useful to see the overall class structure. Who is the owner of the picker? Who is the owner of that owner? What is the logic of allocation/deallocation of the nodes in that structure?
spbfox