0
@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
  • (void) dealloc { [_view release]; [super dealloc]; [student dealloc]; }@end

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?????

ABHI...
  • 39
  • 6
  • Ask yourself why you aren't using ARC (automatic referencing counting). – rmaddy Sep 23 '14 at 18:33
  • And find a modern tutorial. There is no need for the `@synthesize` line. – rmaddy Sep 23 '14 at 18:34
  • 1
    Semi-unrelated, but if you aren't using ARC, and you override -dealloc like you're doing there, you MUST call [super dealloc] at the end of it. Otherwise you will leak the object. – Catfish_Man Sep 23 '14 at 18:40
  • [super dealloc]....in this case deallocation will be done on super object...why not put it here [self dealloc]; – ABHI... Sep 23 '14 at 18:42
  • like say Student *student=[[Student alloc]init]; then [student release]; it and use dealloc like above then [super dealloc] dealloc method deallocate which object???? and why we have to deallocate super object??? – ABHI... Sep 23 '14 at 18:46

1 Answers1

2

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;
danh
  • 62,181
  • 10
  • 95
  • 136
  • like say Student *student=[[Student alloc]init]; then [student release]; it and use dealloc like above then [super dealloc] dealloc method deallocate which object???? and why we have to deallocate super object??? – ABHI... Sep 23 '14 at 20:48
  • Dealloc is a confusing name. It doesn't deallocate the object, it warns the object that it is about to be deallocated giving it the object a chance to release anything its retaining. Calling super dealloc invokes the inherited dealloc, which would release properties that are defined by the superclass. – danh Sep 23 '14 at 20:58
  • Here's a good doc on the subject, written after ARC was introduced. The modern way to do this is to use ARC which simplifies a lot. But its fine that you're learning about the old way, since it will help you understand ARC better. http://rypress.com/tutorials/objective-c/memory-management.html – danh Sep 23 '14 at 21:00
  • which sort of properties...can u tell me about that..?? i some method like init they also called on object that present in super class...so that's why i have to tell [super dealloc] ?? – ABHI... Sep 23 '14 at 21:02
  • You might declare a property as retained... @property(retain) id foo; For properties declared like that, the synthesized setter releases the old value and retains the new value when you call it. Dealloc gives you the chance to release all of the current properties one last time before you die. And your superclass may have retain properties too. Since you override dealloc, you need to give your superclass a chance to clean up too. – danh Sep 23 '14 at 21:08
  • but why super class will retain that property here in this code @interface String :NSString why NSString will retain that property?? – ABHI... Sep 23 '14 at 21:19
  • Your superclass might retain other properties, ones you might not even know about. If it doesn't retain anything (which might be the case), then [super dealloc] does nothing, but you should still call it. And don't subclass NSString (see here http://stackoverflow.com/questions/19469695/subclass-nsstring). Memory management is a rich subject, much too big to cover well in SO chat. – danh Sep 23 '14 at 21:24