tags:

views:

68

answers:

1

As you can see in the code below I am creating and initialising a Vertex which I then add into a NSMutableArray instance variable within my Frame object. As I currently have this setup myVert is owned by main and pointed to by vertexList. Would I be better setting this up so that I make a copy of inVertex within the addVertex method so the object takes ownership of its data?

-(void)addVertex:(Vertex*) inVertex {
    [vertexList addObject:inVertex];
}

// -------------------

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Frame *myFrame;
    Vertex *myVert;

    NSLog(@"MDD_ObjectTest ... ");
    myFrame = [[Frame alloc] init];
    myVert = [[Vertex alloc] initWithxPos:111 yPos:222 zPos:333];

    [myFrame addVertex:myVert];
    [myVert release];

    // Clean up
    [myFrame release];
    [pool drain];
    return 0;
}

Finally if I should make a copy what is the best way to do that, should I be looking at ...

-(void)addVertex:(Vertex*) inVertex {
    [vertexList addObject:[inVertex copy]];
}

Although I am a little unsure what to do in terms of the Vertex object with regards to copyWith Zone.

gary

+3  A: 

This depends on if Vertex is mutable or not.

If Vertex is immutable:

Since the inVertex can not have any of its data changed, the current way you have it is fine. The vertexList will retain inVertex when you add it and will release it when you remove it. Since the properties of inVertex can not be changed there is no difference in storing it versus a copy.

If Vertex is mutable:

You should store a copy so that the changes to inVertex do not affect the vertex stored in the list. Note however that you have a possible memory leak in the way you have it right now. When you copy an object that copy's retain count is set to 1, then when you store it in the vertexList the retain count becomes 2. When you remove it from the list it will have a retain count of 1, causing a memory leak unless you remember to release it. The best place to release it would be in the addVertex method after it is added to the vertexList to keep its retain count at 1, since only one thing has a reference to it.

- (void) addVertex:(Vertex *) inVertex {
    Vertex * copy = [inVertex copy];
    [vertexList addObject:copy];
    [copy release];
}

Note that Vertex must implement the NSCopying protocol for this to work.

I suggest using the immutable approach unless you have a real valid reason to make vertex mutable other than convenience.

Edit: Regarding copyWithZone:

To implement object copying you have to implement the NSCopying protcol that defines the copyWithZone: method. The only consideration you need to make with the zone is that instead of calling the alloc method on the Vertex class you call the allocWithZone: method instead.

- (id) copyWithZone:(NSZone *) zone {
    Vertex * copy = [[Vertex allocWithZone:zone] initWithxPos:x yPos:y zPos:z];
    // Any other copying that needs to be done
    return copy;
}
Brandon Bodnár
Many thanks, much appreciated ...
fuzzygoat