views:

59

answers:

2

The following code is on apple's website.

1) Inside the setMyArray method, is it necessary to release myArray before setting a new value to it? I thought setting the value of an object to a new object, will release the old object from memory.

2) why does it say myArray = [newArray mutableCopy];, instead of simply saying myArray = newArray;? Is it necessary to pass a mutable copy to our property?

@interface MyClass : NSObject {
    NSMutableArray *myArray;
}
@property (nonatomic, copy) NSMutableArray *myArray;
@end

@implementation MyClass
@synthesize myArray;

- (void)setMyArray:(NSMutableArray *)newArray {
    if (myArray != newArray) {
        [myArray release];
        myArray = [newArray mutableCopy];
    }
}
@end

EDIT: Would it be the same if myArray was (nonatomic, retian)

Apple Documentation
copy
    Specifies that a copy of the object should be used for assignment. (The default is assign.)
    **The previous value is sent a release message**.
+1  A: 

With respect to #1, yes you must release myArray before setting a new value to it, otherwise you'll leak. Note the property, it's labelled as a copy, meaning that myArray will hold an object with a reference count of one once its set.

And with regards to #2, because if you don't mutableCopy, you're not getting a copy of the object, you're simply pointing at the other thing. So, if the other thing goes away, you'll have a pointer to a dangling object. Bad things ensue from that point forward.

jer
Simply retaining the array would avoid the dangling pointer issue. The copy is there so that if they make further modifications to the array on their end it won't affect your ivar.
cobbal
thank you for the answer. But would it be the same if I used retain instead of copy?
aryaxt
No, not exactly the same. You'd avoid the memory issue I define above, yes, but using a *copy method instead of retain is for your app to decide when either is appropriate. I.e., in the above example, I would use retain instead of mutableCopy, but if we were talking strings rather than arrays, I almost always copy rather than retain a string property. There are cases when copying an array makes sense, but there are also cases when retaining an existing array also makes sense. Depends on your program.
jer
A: 

1 - In order to avoid a memory leak you must release the array before setting a new value to it.

2 - mutableCopy is required in the context you provided in order to geta copy of the object, and not just a pointer. If the original array goes away, you'd be left with a dirty pointer.

FYI, copies in Objective-C are shallow, not deep copies. This means that when you do [array copy] or [array mutableCopy], you get an array which is separate from the original array, but which still points to all the same objects. If you want to perform a deeper copy, you'll have to do it manually, for example by iterating through the original array, making copies of the contents, and adding those copies to a new array.

GotDibbs