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, 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];
halfer
  • 19,824
  • 17
  • 99
  • 186
Jayshree
  • 281
  • 1
  • 6
  • 28
  • 2
    Maybe you should tell us why you think this leaks. And some context for the code... self.m_pImagePicker probably retains it, do you release it somewhere? – Eiko Aug 19 '10 at 13:17
  • As Eiko says, if the @property for m_pImagePicker has (retain), you will leak the object if you don't set self.m_pImagePicker to nil somewhere. – Kalle Aug 19 '10 at 13:31
  • What are you doing when the picker is finished? Can you show us the image picker cleanup code there please? – Kendall Helmstetter Gelner Aug 19 '10 at 20:11
  • hi. yes the @property has retain. ie- @propert(nonatomic,retain) uiimagepickercontroller m_pImagePicker. And i have also released m_pImagePicker in dealloc method. so i dont understand where am i going wrong. – Jayshree Aug 20 '10 at 08:54
  • 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 Aug 23 '10 at 16:20

1 Answers1

2

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
  • 939
  • 4
  • 8
  • 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 Aug 20 '10 at 14:07