Q:

I’m confused about “anonymous iOS memory”

I have three ways of writing this code. The third way confuses me.
First way works fine.
//.h
@property (weak, nonatomic) IBOutlet UIImageView *picImageStage;
//.m
NSString *name = [NSString stringWithFormat:@"allen.png"];
UIImage *image = [UIImage imageNamed:name];
UIImageView *t = [[UIImageView alloc]initWithImage:image];
self.picImageStage = t;

Second way works fine.
//.h
@property (retain, nonatomic) IBOutlet UIImageView *picImageStage;
//.m
NSString *name = [NSString stringWithFormat:@"allen.png"];
UIImage *image = [UIImage imageNamed:name];
self.picImageStage = [[UIImageView alloc]initWithImage:image]; 

Third way turns wrong.
//.h
@property (weak, nonatomic) IBOutlet UIImageView *picImageStage;
//.m
NSString *name = [NSString stringWithFormat:@"allen.png"];
UIImage *image = [UIImage imageNamed:name];
self.picImageStage = [[UIImageView alloc]initWithImage:image]; 

I don’t understand the reason. Could anyone help me? Thanks :D

A:

In the 3rd snippet you have declared the @property as weak, and the UIImageView will be deallocated immediately. Because a weak relationship will be nil'd when there is no strong relationship to the same object.
In the 1st snippet, which is almost the same, you assigned the UIImageView to a local variable first. This local variable uses a strong relationship implicitly. If you leave the scope of the local strong variable (i.e. the method where you run this code) the property will be deallocated too, except if you create another strong assignment before leaving the scope of the variable. Which will for example happen if you add the UIImageView as a subView of another view. Adding a view to another creates a strong relationship. 

A:

Let's examine all three, because that might be the easiest way to explain this to you.

Number 1

//first method
//.h
@property (weak, nonatomic) IBOutlet UIImageView *picImageStage;
//.m
NSString *name = [NSString stringWithFormat:@"allen.png"];
UIImage *image = [UIImage imageNamed:name];
UIImageView *t = [[UIImageView alloc]initWithImage:image];
self.picImageStage = t;

You're initializing a local variable UIImageView with a retain count of +1.  Because weak properties don't call implicit retains on their values, you don't own the value stored in self.picImageStage, which means you're one lucky camper because as soon as the function that declares that local UIImageView passes out of scope, your variable is deallocated.

Number 2

//second method
//.h
@property (retain, nonatomic) IBOutlet UIImageView *picImageStage;
//.m
NSString *name = [NSString stringWithFormat:@"allen.png"];
UIImage *image = [UIImage imageNamed:name];
self.picImageStage = [[UIImageView alloc]initWithImage:image]; 

This one gets into those implicit retains I was talking about.  The compiler expands this line:
self.picImageStage = [[UIImageView alloc]initWithImage:image]; 

out to 
self.picImageStage = [[[UIImageView alloc]initWithImage:image]retain]; 

Meaning that you own it, and are free to do with it what you please.

Number 3

//third method
//.h
@property (weak, nonatomic) IBOutlet UIImageView *picImageStage;
//.m
NSString *name = [NSString stringWithFormat:@"allen.png"];
UIImage *image = [UIImage imageNamed:name];
self.picImageStage = [[UIImageView alloc]initWithImage:image]; 

Not only are you assigning to a weak property from a variable that you don't own, as soon as the assignment is called, your variable won't store the value!  It's because, again, weak pointers do not call implicit retains on their assignments, meaning you have no control over how long the variable sticks around, and I'm willing to bet that it doesn't stay alive for very long!

