Reputation: 157
If I do this:
1 NSMutableArray *near = [[NSMutableArray alloc] init];
2 NSMutableArray *all = [[NSMutableArray alloc] init];
3 NSMutableArray *current = near;
4 current = all;
What happens to near
?
At line 3, am I setting current to point to the same address as near so that I now have two variables pointing to the same place in memory, or am I setting current to point to the location of near in memory such that I now have this structure:
current -> near -> NSMutableArray
The obvious difference would be the value of near at line 4. If the former is happening, near is untouched and still points to its initial place in memory. If the latter is happening,
Upvotes: 1
Views: 2071
Reputation: 45158
current = all;
is just assigning all to current. Hence, you still have to release all
when you are finished; [all release];
. The same when assiging near to current; (Because you create them using alloc init
)
Is exactly the behavior you get when you declare a property with assign
attribute with the only difference you don't use self
to access current
:
@property(nonatomic, assign) NSMutableArray *current;
you synthesize it:
@synthesize current;
and then assign:
NSMutableArray *near = [[NSMutableArray alloc] init];
NSMutableArray *all = [[NSMutableArray alloc] init];
...
self.current = all; //in your program you will do: current = all;
self.current = near; //in your programm you will do: current = near;
...
[all release];
[near release];
in your dealloc method:
current = nil; //this is not necessary but good to do.
//see I am not releasing 'current'?
Alternatively, there is the retain approach (Explained by @Anders):
@property(atomic, retain) NSMutableArray *current;
self.current = all; //will be similar to: current = [all retain];
and in that case you will need to release current
later
Upvotes: 0
Reputation: 36092
1 NSMutableArray *near = [[NSMutableArray alloc] init];
you have created an NSMutableObjectArray with retain count 1, 'near' points to it
2 NSMutableArray *all = [[NSMutableArray alloc] init];
you have created an NSMutableObjectArray with retain count 1, 'all' points to it
3 NSMutableArray *current = near;
'current' now points to the same object as 'near' does, i.e. the NSMutableArray with retain count 1
4 current = all;
'current' now points to the same object as 'all' does, i.e. the NSMutableArray with retain count 1
note the retain count, it is normally good when you reference an object to increase the retain count in order to be sure the object is still there if the other variable is released:
current = [all retain];
...
[current release];
Upvotes: 4
Reputation: 20163
You are setting current
to point to the same location in memory that near
is pointing to. In order for current
to point to the memory location of near
, it would have to say:
current = &near;
Upvotes: 1
Reputation: 53561
At line 3, current and near point to the same object (location in memory). You can simply log the memory addresses of both objects to validate this:
NSLog(@"current: %p / near: %p", current, near);
At line 4, near will still point to the same object.
Upvotes: 0