Reputation: 6940
Its very common question everybody familiar with, but I'm still not understand it fully.
If Object A owns (have a strong property of) Object B, and Object B have a strong property of object A, there is retain cycle, and no object can be released and there is a memory leak.
But, what if Object A instead will point for Object C instead of Object B, therefore another address in memory?
As far as i know, strong properties do something like follow:
- (void)setObject:(id)newObject{
if (_newObject == newObject){
return; //
}
NSObject *oldObject = _newObject;
_newObject = [newObject retain];
[oldObject release];
}
So, what if we point instead for Object C, isn't in that case memory for Object B will be released? What if both Objects (A and B) will instead set nil
object? Is there still would be retain cycle with memory leak? With old value "floating" somewhere in memory?
I know, that has been discussed many times, but i still can't get "whole picture" in my head. I would appreciate any clarification in that matter.
Upvotes: 1
Views: 366
Reputation: 86651
If the question is "can you break a retain cycle by reassigning one of the properties?", the answer is yes. This applies equally if you assign nil to the property.
Upvotes: 3
Reputation: 52548
The word you are looking for is "retain cycle".
The simplest retain cycle would be an object having a strong reference to itself. Very rare because it is rather pointless.
The most common case is A having a strong reference to B and B having a strong reference to A. A->B->A. You see the cycle?
The cycle can be any length A->B->C->D->E->F->G->...->A. It's a retain cycle, so nothing will be released. If you have just A->B->C->D->...->Z with no reference back to another object in the sequence, then there is no cycle. No cycle, no problem.
Upvotes: 3