do i have to retain a member variable?

Discussion in 'iOS Programming' started by samgeribo, Jul 26, 2008.

  1. macrumors newbie

    Jul 26, 2008
    I have a class with a NSMutableArray* declared in the header file. I am not using a property for it. In the constructor I have:

    -(id) initWithWidth:(int)_width Height:(int)_height Friction:(float)_friction TimeDelta:(float)_timeDelta {
    if(self = [super init]) {
    balls = [NSMutableArray array];
    [balls retain];
    width = _width;
    height = _height;
    friction = _friction;
    timeDelta = _timeDelta;

    return self;

    My question is that if I take out the [balls retain] line my program crashes but if I leave it in it works great. Why do I have to retain it? I thought the NSMutableArray:array method automatically set the retain count to 1.
  2. macrumors member

    Mar 21, 2008
  3. thread starter macrumors newbie

    Jul 26, 2008
    I thought the convenience method did a retain

    Thanks for the quick reply!

    I am still confuzzled. I know your answer is the right answer, but I'd like to have a deeper understanding. I thought the convenience methods did something like this:

    + NSMutableArray:array {
    NSMutableArray* array = [[NSMutableArray alloc] init];
    return [array autorelease];

    The alloc method should set the retain count to 1. So why should I have to do a retain in my code?

    On the other hand, if this method really did an autorelease without doing a retain that would seem to violate Apple's own memory management policies.
  4. Moderator emeritus


    Jun 15, 2000
    alloc gives it a retain count of 1. autorelease doesn't change its retain count, but it puts it into the autorelease pool which gets emptied at a later point. You must retain an autoreleased object if you want to keep it around.
  5. macrumors member

    Mar 21, 2008
    You can also create a property with the retain flag and upon assignment your object will be retained. (Make sure to release it in dealloc.)

    @interface MyObject : NSObject {
        NSMutableArray *balls;
    @property (nonatomic, retain) NSMutableArray *balls;
    - (void)initBalls;

    #import "MyObject.h"
    @implementation MyObject
    @synthesize balls;
    - (void)initBalls {
        balls = [NSMutableArray array];
    - (void)dealloc {
        [balls release];
        [super dealloc];
  6. macrumors member

    Jul 28, 2008
    In that case you'd need to call self.balls = [NSMutableArray array]. Simply assigning the ivar won't retain the object properly (since it won't call the setBalls: method).
  7. macrumors 6502

    May 9, 2008

    My doubt is whether such non-retaining codes will call applicationDidiRecievedMemoryWarning: method ????

    My app is not crashing at a particular point.... it can happen at any point of time.... So i cant point out where its crashing... But its not crashing during the debug process, only after deployment, Once i found that it is calling the applicationDidiRecievedMemoryWarning method: But my app is taking only a maximum of 3MB during runtime....
  8. macrumors member

    Jul 28, 2008
    If your app is "randomly" crashing with EXC_BAD_ACCESS, from my experience it is most often due to an object being dealloc'ed too early, because you didn't retain it when you should. I don't see how applicationDidReceiveMemoryWarning:, if empty, could change anything in this matter.

Share This Page