Line 1: First.h
Line 2: @property (nonatomic, retain) NSString *name;
Line 3: First.m
Line 4: @synthesize name;
Line 5: -(void)viewDidLoad()
Line 6: {
Line 7: name = [NSString stringWithString:@"Hello"];
Line 8: OR
Line 9: self.name = [[NSString alloc]initWithString:@"Hello"];
Line 10: OR
Line 11: }
Line 12:-(void)dealloc()
Line 13: {
Line 14: [name release];
Line 14: }
Question 1:
If I follow line number 7 everything is fine and if I used line number 9 Memory leakage is there. As I know self is point to current object and if I used self.object
or simply object
no more difference.
Question 2: And if I used
@synthesize name = _name;
Then which one is for setting the value of name and which one is for get the value? Difference between:
name = [NSString stringWithString:@"Hello"];
OR
self.name = [NSString stringWithString:@"Hello"];
OR
_name = [NSString stringWithString:@"Hello"];
Question 3:
If I create any property is there any need to alloc in the memory as I allocated showing the memory leakage?
QUESTION 1: if I follow line number 7 everything is fine and if I used line number 9 Memory leakage is there. As I know self is point to current object and if I used self.object or simply object no more difference.
Line 7: name = [NSString stringWithString:@"Hello"];
Line 8: OR
Line 9: self.name = [[NSString alloc]initWithString:@"Hello"];
On line 7 you are using a convenience constructor which returns an autoreleased object and assigning the object directly to your name
ivar; now, it is ok to assign an autoreleased object to a retain property, but it is incorrect to assign an autoreleased object directly to an ivar without also explicitly retaining it:
name = [[NSString stringWithString:@"Hello"] retain];
On line 9 you are using alloc/init which gives you a retained object: it is correct to assign such an object directly to an ivar, but you should autorelease it before assigning to a retain property, e.g.:
self.name = [[[NSString alloc]initWithString:@"Hello"] autorelease];
You can reason about this in terms of the object retain count:
a convenience constructor will set the retain count to 1, but this will be later on automatically decreased by a release
call done by the framework;
alloc/init will give you a retain count of 1 that will not be decreased unless you explicitly call release
;
When an object retain count goes to 0, then it will be deallocated.
Reasoning in terms of retain count is just a way to look at this whole matter of memory management and to understand what is going on deep in the framework; in no case this is a proper way to analyze your objects lifecycle, though.
then which one is for setting the value of name and which one is for get the value? Difference between
name = [NSString stringWithString:@"Hello"];
ORself.name = [NSString stringWithString:@"Hello"];
OR_name = [NSString stringWithString:@"Hello"];
name = [NSString stringWithString:@"Hello"];
and
_name = [NSString stringWithString:@"Hello"];
are just the same thing in the two cases given. This will bypass the property setter(/getter) method and assign directly to the ivar. In this case, your app would sooner or later crash because you are assigning an autoreleased object to the ivar directly. This would be correct:
_name = [[NSString stringWithString:@"Hello"] retain];
or
_name = [[NSString alloc] initWithString:@"Hello"];
Note that in a program where you declare your ivar as _name
, you cannot use name
to refer to it; you can use name
to directly refer to the ivar if you declared only the property without explicitly specifying the ivar like you did for question 1 (in this case, name
would be the ivar automatically generated by the compiler for you).
On the other hand:
self.name = [NSString stringWithString:@"Hello"];
will use the property accessor method (actually, the setter); since your property is declared as retain
, it is ok to assign to it the autoreleased variable returned by the convenience constructor stringWithString:
.
QUESTION 3: if I create any property is there any need to alloc in the memory as I allocated showing the memory leakage.
This is not really clear to me.
A good document where to read about the basic of memory management is: Memory Management Policy and also Practical Memory Management from Apple's Advanced Memory Management Programming Guide.