When I create a variable I want to assign to a property I can do the following (assuming the property is @property (nonatomic,retain) UILabel *myLabel;):
UILabel *temp = [[UILabel alloc] init];
self.myLabel = temp;
[temp release];
What would happen in the following scenario where temp is not used?
self.myLabel = [[UILabel alloc] init];
[myLabel release];
This is assuming I would add a [myLabel release]; in dealloc due to the property.
Would this be proper memory management? In the second example does myLabel have a retain count of 2 after the init line?
The second might happen to work, but is incorrect. The first line calls
setMyLabel:. That may happen to set an ivar, it may not. It might do all kinds of things (it might make a copy, it might store the information elsewhere, etc). Your second line releases anivar. If the setter happens to be implemented the way you’re assuming, then you will get lucky and it will work. But this is not correct memory management.The first example is correct. You can also use the autorelease pool to simplify things. Better is to move your code to ARC, which solves all of these problems faster and more easily.