objective c - iPhone Memory Management and Releasing -
Here's a general practice that I often see (which includes a very popular iPhone developer book)
.h file:
@interface Some ViewController: UIViewController {UIImageView * imgView; } Somewhere in the .m file And later, we see it ... Since imgView has a mail allocation and release, does the release of imgView in dealloc be required?
- (zero) Delok {[imgView release]; [Super DeLoc]; }
Yes, there are problems with that code; The imgView release which could possibly cause accidents in rare circumstances maintains an object in the frequency variable without storing anything, and it is usually running incorrectly about memory management.
There will be a right way to do this:
@interface Some ViewController: UIViewController {UIImageView * imgView; } @property (nontomic, retten) UIImageView * imgView; and in implementation;
@ synthesis imgView; From anywhere in the module:
// Create a new image view object and keep it in the local variable (keep the count 1) UIImageView * newImgView = [[UIImageView alloc] initWithFrame: self.view.bounds]; NewImgView.image = [UIImage imageNamed: @ "someimage.png"]; // Use to store our new image view as an instance variable, // If the old value of IMGview is present, then it will be issued by the generated method, and our new IMVview will be maintained ( Count 2 will be maintained) self.imgView = newImgView; // Release the local variable, as the new UIImageWil is safely stored in the // IMGView instance variable. (1 count is retained) [New IMGview release]; // See the new IMView for the main image, it will maintain that the calculation will increase, and the UIMAJV will remain in memory until it is released by both the main menu and this controller. (Count 2 remains intact) [self.view addSubview: self.imgView]; and Delok remains the same:
- (zero) DLock {[imgView release]; [Super DeLoc]; }
Comments
Post a Comment