Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alloc + init with synthesized property - does it cause retain count to increase by two?

I've seeen the following snippet quite a bit:

In the header:

SomeClass *bla;
@property(nonatomic,retain) SomeClass *bla;

In the implementation file:

@synthesize bla;

and then

self.bla = [[SomeClass alloc] init];

I think that this assignment puts the retain count for 'bla' up by two; once through the alloc/init call, then through the retain that we asked to happen through the synthesized property setter.

As a result, I normally declare my properties like this:

In the header:

SomeClass *_bla; // note the underscore
@property(nonatomic,retain) SomeClass *bla;

In the implementation file:

@synthesize bla = _bla;

and then

_bla = [[SomeClass alloc] init];

Provided my initial assumption is correct - I'd be interested to hear whether there is 'right' way to do this, i.e. the declaration, initialisation and memory management of properties?

like image 421
patschiboy Avatar asked Jul 16 '10 06:07

patschiboy


2 Answers

Yes, you are right - using the synthesized setter of a retain property would increase the ref-count on an instance you already own (as alloc implies ownership).

Just go with the second form you mentioned in your initializers:

_bla = [[SomeClass alloc] init];

... and remember to fix the retain count otherwise, e.g.:

self.bla = [[[SomeClass alloc] init] autorelease];
like image 126
Georg Fritzsche Avatar answered Sep 28 '22 07:09

Georg Fritzsche


I think that this assignment puts the retain count for 'bla' up by two;

True.

I'd be interested to hear whether there is 'right' way to do this

Your last piece of code is the right way, but the leading underscore is not recommended. The property and the ivar can share the same name. Just

@interface Foo : Bar {
  SomeClass* bla;
}
@property (nonatomic, retain) SomeClass* bla;
@end

@implementation Foo
@synthesize bla;
-(id)init {
   ...
   bla = [[SomeClass alloc] init];
   ...
}
-(void)dealloc {
  [bla release];
  ...
  [super dealloc];
}

is enough.


Some people may use

SomeClass* foo = [[SomeClass alloc] init];
self.bla = foo;
[foo release];

or

self.bla = [[[SomeClass alloc] init] autorelease];

in the -init method, but I strongly discourage it, as this calls unnecessarily many methods, and you cannot guarantee the behavior of the setter.

like image 38
kennytm Avatar answered Sep 28 '22 08:09

kennytm