Profo
Profo

Reputation: 153

Is that the correct way to deal with ivars?

I have read a lot of topics about getters and setters. I know what they are and why are useful. Different source claim's different ways to release the ivars. Here begins my confusion

@interface CoolClass : NSObject
{
  NSString *name;
}

@property (nonatomic, copy) NSString *name;
@end

@implementation CoolClass
@synthesize name = _name;

-(id)init
{
  if(super = [self super])
  {
     self.name = @"Jo";
  }
  return self;
}

-(void)dealloc
{
  [self.name release], self.name = nil;
}
@end

Is that the correct way to release/free ivars ?

Upvotes: 3

Views: 211

Answers (3)

justin
justin

Reputation: 104698

You'll want to use accessors most of the time, but not in partially constructed states because they can have negative side-effects. Here's how it's done:

- (id)init
{
  if((self = [super init])) {
     // self.name = @"Jo"; << don't use accessors in initializer

     _name = [@"Jo" copy]; << good
  }
  return self;
}
// added for another variation:
- (id)initWithName:(NSString *)inName
{
  if((self = [super init])) {
     _name = [inName copy];
  }
  return self;
}

- (void)dealloc
{
    // don't use accessors in dealloc
    // don't release the result of a getter (release the result of the retained or copied result)
    // [self.name release], self.name = nil;  

    // instead:
    [_name release], _name = nil;
    [super dealloc]; << compiler should have warned you about this one
}

Note: In the case of init, the string literal is an immortal and it won't matter if you copy it because the copy just returns itself. My preference is to just 'copy' the immortal for clarity, although it's unnecessary.

Upvotes: 2

nagan
nagan

Reputation: 119

@interface CoolClass : NSObject
{
  NSString *name;
}

You declared here an instance variable 'name'; Nowadays there is no need to declare ivars in the header file. Just use properties and make the compiler to synthesize ivar for you.

@property (nonatomic, copy) NSString *name;

Here we have a property declaration that specifies that a copy of the object should be used for assignment and that a previous value is sent a release message.

In implementation you want to synthesize your property:

@synthesize name = _name;

This code tells the compiler to generate a getter and setter for property called 'name' and use instance variable called '_name' to store value. So you have now two ivars - 'name' and '_name'.

That how init method should like like:

-(id)init
{
  if(self = [super init])
  {
     name = @"This is ivar declared between {}";
     _name = @"synthesized ivar";

  }
  return self;
}

And the dealloc:

-(void)dealloc
{
  [name release];
  [_name release];
  [super dealloc];
}

Upvotes: 1

Nick Lockwood
Nick Lockwood

Reputation: 40995

Here is what I would advise:

@interface CoolClass : NSObject
@property (nonatomic, copy) NSString *name;
@end

@implementation CoolClass
@synthesize name = _name;

-(id)init
{
  if(super = [self super])
  {
     self.name = @"Jo";
  }
  return self;
}

-(void)dealloc
{
  [_name release];
  [super dealloc];
}

@end

Notes:

  1. There is no need to explicitly declare ivars inside { ... } in your header. They will be created automatically when you synthesise your property. Explicit ivars are a legacy concept that are no longer needed since about iOS 3.

  2. You should not use self.name in the dealloc as this calls the getter method, which may do additional work beyond merely fetching the ivar. Normally it's good practice to use the getter method, but in the dealloc you should release the ivar directly

  3. It is good practice to set ivars to nil after releasing them, but again in the dealloc this in not necessary because no code is ever executed after dealloc, so the pointer won't be referenced again.

  4. Normally (outside of the dealloc), if you wish to release an ivar you should set it to nil using the setter like this: self.name = nil; that will automatically release it and set it to nil. This is equivalent to [_name release], _name = nil;

Upvotes: 1

Related Questions