Basic question here.
Say in my .h, I define a NSString:
@interface MyGreatClass
@property (nonatomic, retain) NSString *myAwesomeString;
@end
And then in my .m, I have the following code:
@implementation MyGreatClass
@synthesize myAwesomeString
-(void)dealloc{
[myAwesomeString release];
}
-(void)viewDidLoad{
self.myAwesomeString = [[NSString alloc] initWithString:@"Yay"];
}
@end
-
Did I leak? I know, I know, whenever you call
retainoralloc
on a method, it increases the counter in memory by one and you need
to have a release statement for eachretainandalloc, so I
suspect I do, but I am just making sure because if this is true, I
have to do this for a crapload of variables in my app. Also if you
do have to call release twice, where do I call the second release?
Can I call both of them indealloc, one right after the other? -
How nesesarry is the
self.in front of variables when referring to properties of the class
you are in? What happens if you don’t have it and when exactly is it
necesarry?
Yes, that will leak because it’s overretained. The four standard correct solutions are:
You’ll notice the last one doesn’t start with
self., which brings us to your second question. If you don’t writeself., you are not accessing a property. ThatmyAwesomeStirngin Solution 4 is an instance variable, accessed directly. It won’t go through the property accessors, so it won’t retain the value you assign and it won’t release any value that the variable held before.