While Daniel's answer is correct in addressing your question, it does not cover what you should do.
This is how your code would be written in a modern world:
turn on ARC. Especially if you are learning or this is your first project. There is no reason to not use ARC. Learning manual-retain-release is valuable, but not critical at this time as the tools do a very good job of providing analysis of when ARC based patterns are leaking memory (either the analyzer or using Instruments, both of which you would need to use under MRR and neither of which work as well under MRR).
Don't use @synthesize and don't declare iVars (and certainly don't declare iVars in your .h file). Let the compiler autho-synthesize the _
prefixed ivars automatically. The _
prefix has the added advantage of disallowing you from accidentally directly accessing an ivar in code. I.e. self.foo
vs. foo
; the second won't compile.
Or to translate into code:
@interface MyClass : NSObject
@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UITabBar *tabBar;
@property (nonatomic, retain) UIWebView *webView;
@end
And:
@implementation MyClass
{
BOOL _flag;
UIImageView *_imageView;
}
- (id)init {
self = [super init];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
}
return self;
}
// no -dealloc
@end
Note that I declared _imageView
as an instance variable that will be compatible with the obvious imageView
@property
should you need to refactor your class later to make that externally available.
If you realy must use manual retain-release, then add a -dealloc
method that calls -release
on all the ivars. I.e.[_view release];
, [_imageView release];
, etc...
Don't get what you mean by "Note that I declared _imageView as an
instance variable that will be compatible with the obvious imageView
@property should you need to refactor your class later to make that
externally available."
If you were to decide that _imageView
needs to be accessible to other objects, then you would delete the iVar declaration and add:
@property(nonatomic, retain) UIImageView *imageView;
The compilers automatic synthesis would create an instance variable named _imageView
automatically and none of the rest of your code would have to change.
Do we have to make sure an ivar is not nil before releasing it in
dealloc method? (See Update 3 above.)
No. In Objective-C, nil eats messages. That is, [nil release];
is perfectly valid and does absolutely nothing at runtime.
In your code the BOOL flag; has disappeared. Do we have make a
property for BOOL flag;, that is, @property BOOL flag;? Or all we have
to do is just placing a private field in the MyClass.h as @private
BOOL flag; up there in my original question?
I forgot it. You could create a property for it. Or you could declare _flag
as an iVar next to _imageView
as I've done above.
Most importantly, there is no longer any reason (save for a very rare case that is generally too be avoided) to declare instance variables in your .h.