My question is if I should release the instance variable "distanceSlider" in the init method (marked red)?
By adding the UISlider as a subview to the content view of my table cell the retain count of the instance variable is increased +1? So should I release distanceSlider after I added to the subview?
The Apple MemoryMgmt guide says that variables should only be released if they were created with a method's name that begins with “alloc” or “new” or contains “copy”.
Assuming that distanceSlider is a @property, then no you should not release it there. It will be released in dealloc as you already have.
@BrianSlick:
You're right distanceSlider is a property.
I was asking if distanceSlider has to be released twice because I thought it was responsible for the retain count of the instance variable. And when I add the slider as subview to the contentView of my TableCell the retain count is increased +1.
But as I understand it now, each pointer is responsible for releasing itself from the pointed address. So in this case, distanceSlider releases its pointer in the dealloc method. And the TableCell in the class in which it will be used.
I hope I've explained it right.
The +1 you saw was from addSubview. The parent view is retaining its subviews. You do not need to worry about that. If you later remove it from that view, or if it still exists when that view is deallocated, a release message will be automatically sent at that time.
Your responsibility is to balance your memory-related actions - alloc, copy, new, retain - with a release.
It is not your responsibility to guess how Apple's classes work. The implementation for addSubview could retain your slider one time, or it could retain it many times. You have no way of knowing. You have to trust that whatever Apple's classes DO, they will also UNDO appropriately. Same for arrays, dictionaries, etc. The act of adding an object to an array, just like adding your slider to a view, does not involve alloc, copy, or new (unless you do it wrong), so it should not be released.
The +1 you saw was from addSubview. The parent view is retaining its subviews. You do not need to worry about that. If you later remove it from that view, or if it still exists when that view is deallocated, a release message will be automatically sent at that time.
Your responsibility is to balance your memory-related actions - alloc, copy, new, retain - with a release.
It is not your responsibility to guess how Apple's classes work. The implementation for addSubview could retain your slider one time, or it could retain it many times. You have no way of knowing. You have to trust that whatever Apple's classes DO, they will also UNDO appropriately. Same for arrays, dictionaries, etc. The act of adding an object to an array, just like adding your slider to a view, does not involve alloc, copy, or new (unless you do it wrong), so it should not be released.