objective c - iPhone basic memory management -
Is it proper storage management? I am thinking that if alloc
is in -viewDidLoad
.
should be released
after, SomeViewController.h
#import & lt; UIKit / UIKit.h & gt; @ Interface Some ViewController: UIViewController {UIButton * someButton; NSString * someString; } @ Property (Nonomatic, Replacement) IBOutlet UIButton * someButton; @property (nontomic, copy) NSString * someString; @end
Some ViewController.m
#import "SomeViewController.h" @implementation SomeViewController @ synthesis someButton, someString; - (zero) viewDidoadload [[Super Viewedload]; SomeButton = [[UIButton alloc] init]; SomeString = [[NSString alloc] init]; } - (zero) viewDiduude {self.someButton = zero; Self.someString = Zero; } - (Zero) DailyButton release, self.someButton = Zero; [SomeString release], self.someString = Zero; [Super DeLoc]; } @end
Thanks
Edit: One more thing if I put UIButton
in IB, So do I still need alloc
?
When you call init on an object, the count of maintaining becomes 1. You have two different setter features: There is a "maintain" for your UIButton, and the second is "copy" for your "NSString". When you call
self.someButton = someUIButtonObject;
someUIButtonObject receives a retaining message and hence its count increases to 1. In the case of your original code, call release in dealloc will issue a reference for some UIButton objects, but it will still be a indexed number of 1 and thus will be leaked.
There is a different problem with your NSString in the second case but there is still leak memory here [/ for NSString alloc] init] your call is the result in a new string object, and then calling
self.someString = someNSStringObject;
As a result a brand new object is created which copies the contents of some NSStringObject. In this case, the NSStringObject and calculate a pass went object each copied setter here, because you can assign the string at-ed to leaks which you have now is not it a reference and a There is no scope for maintaining one.
A quick side note: does not know how your actual code for me, but it's pretty useless to be sure that NSStrings irreversible (just [[NSString alloc] init] and UIButton basically you each call Need to be allocated with a call to maintain, copy, or release (or autorealise). It is appropriate
self.someButton = [[[UIButton alloc] init ] Autorelease];
On some unknown times in the future, however the object is released Will. If your memory is very tight and do not you use Otorivetiv is required memory Asapi. In that case you do:
UIButton * tempButton = [[UIButton alloc] init] ; Self.someButton = tempButton; [TempButton release];
in which guarantees that your Otoriarej pool do not have the large object.
in addition, these Always use gates / sets to use properties (manually. Some buttons unlike someButton). This way You do not give a new pointer to a button by mistake and old rigs.
When you create a button in the IB, the XIB keeps a reference to the button (retains +1 count). If you make an IBotlet you can access the button through the program, you can maintain some object, so if you have an IBOlet for some button, then when some button is loaded XIB Holds a number of 2 You do not have to allocate the object, which is done automatically when XIB is loaded in memory In addition, you are only responsible for releasing a reference to the button (your IBotlot Reference). In general, this is a good practice to release your reference as soon as you know that you do not need it anymore. For example, if you know that you need a button and it has to do initial optimization and there is nothing else, then you will probably do something like this:
- (zero) viewDidload { // Customize some buttons [some button release]; }
Otherwise you will probably leave some buttons in the scene. Because you know at that point because you no longer need to reference the button.
Comments
Post a Comment