Objective C memory management question with NSArray

Posted by Robert on Stack Overflow See other posts from Stack Overflow or by Robert
Published on 2010-12-31T13:38:27Z Indexed on 2010/12/31 13:54 UTC
Read the original article Hit count: 139

I am loading an array with floats like this:

NSArray *arr= [NSArray arrayWithObjects:
                [NSNumber numberWithFloat:1.9],
                [NSNumber numberWithFloat:1.7],
                [NSNumber numberWithFloat:1.6],
                [NSNumber numberWithFloat:1.9],nil];

Now I know this is the correct way of doing it, however I am confused by the retail counts.

  1. Each Object is created by the [NSNumber numberWithFloat:] method. This gives the object a retain count of 1 dosnt it? - otherwise the object would be reclaimed

  2. The arrayWithObjects: method sends a retain message to each object.

This means each object has a retain cont of 2. When the array is de-allocated each object is released leaving them with a retain count of 1.

What have I missed?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about memory-management