Suppose that i have UIView viewLoading declared in .h. and i do not directly initialize it (in the first code).
The first Code.
UIView *viewLoading2 = [[[UIView alloc] initWithFrame:CGRectMake(75 , 155, 170.0, 170.0)]];
viewLoading = viewLoading2;
[viewLoading2 release]
The second code:
viewLoading = [[[UIView alloc] initWithFrame:CGRectMake(75 , 155, 170.0, 170.0)]];
The third Code:
- (void) viewLoad:(UIView *) viewLoading2
{
viewLoading = viewLoading2;
//do i need to retain, alloc, or release something here?
}
-
2In the first code, do i need to release viewLoading in dealloc ? And what happen if i do not declare its property?
-
In the second code, does it have same effect from the first code? (need to dealloc or not).
-
For the third code, does it have same effect from the first code? and what should i do after i code that? (see the comment)
-
Do iPhone Code always need to have release for variable declared in .h? Or only if the variable declared in .h is allocated? if like in the first code, do i need to dealloc viewLoading?
-
what is the different between
self.viewloading = viewLoading2;
and
viewloading = viewLoading2;
Thanks
In the first example, you allocated the object (once), and you released it (once), so you don’t need to do anything else. On the other hand, viewLoading is invalid as soon as you send the release to viewLoading2, so it’s not very useful code.
In the second, you have not released viewLoading yet, so it does need to be done eventually.
In the third, whatever code allocated the object that was passed into this method via the parameter is responsible for releasing it. It should be valid for the duration of this method, but if you’re saving it for later use you need to retain it here, then release it when you’re done.
Edit:
I’m not sure I understand your question 4. A declaration in the interface (.h) file is just reserving space for a pointer. It’s not an object declaration, so there’s no release required until you actually do an object allocation.
self.viewloading = viewLoading2is using the properties setter method to do the assignment. If the @property statement has “retain” in it, then a retain is done as part of that assignment. `viewloading = viewLoading2″ is a straight assignment, no retain.