ABHI...
ABHI...

Reputation: 39

Must I manually deallocate properties declared as retain?

@interface Rectangle
@property (retain) UIView *view;
@end

@implementation Rectangle

CGRect frame = CGMakeRect();
self.view = [[UIView alloc] initWithFrame:frame]
Student *student=[[Student alloc]init];
[student release];                          // not using this but using dealloc on it see below

My question is: here why we have to deallocate the memory on super object ???? what happen if we deallocate the memory on student with release it?????

Upvotes: 0

Views: 68

Answers (1)

danh
danh

Reputation: 62676

retain and dealloc are not compliments. retain and release are complements, adding to and subtracting from an object's reference count respectively.

If your synthesized setter does a retain, then your dealloc should do a release (and a [super dealloc]).

The modern approach is to use ARC, drop the @synthesize, and always refer to your properties with the synthesized setters and getters, like this:

id foo = self.property;
self.property = foo;

Except in init, where it's better to say:

_property = foo;

Upvotes: 2

Related Questions