1

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,

4 Answers 4

4
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];
Sign up to request clarification or add additional context in comments.

3 Comments

Holy Cow! That was it! I didn't think I had to do that since I was accessing near via the property: @property (nonatomic, retain) NSMutableArray *near;. Doesn't that automatically retain near?
This is a nice play-play. And, to address the question, the retain in that property means that, when setting it, the new value is retained by the setter. It does not mean that the value returned by the getter is retained.
@Matt: Thanks for the explanation. I guess I have to brush up on properties
1

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;

3 Comments

actually that is not necessary, you can do current= near; as long as you do [near release]; [all release]; later. Assigning it is easy. You just have to care about ownership. ;)
Thanks for the speedy reply, I verified it current and near are pointing to the same object in this example. The problem is in my app they are not. Something else is going on..
@nacho4d: It is necessary. I'm not saying he needs to do that to accomplish his goal, but he was asking if current would be pointing to near. I'm saying that, for that to be the case, the code would have to read current = &near.
0

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.

1 Comment

Thanks, indeed was able to verify they were pointing to the same object. As this was an abstracted issue I was having with my app, I must look elsewhere to figure out why it's not working. I'm not even sure what question to ask besides posting the whole app and asking, "Why the heck won't this work?"
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.