Objective-CStudy NotesUIView memoryRelease is the content to be introduced in this article,UIView memoryReleased. Previously, I thought thatUIViewObject, and finally release or removeFromSuperview. However, in the recent project, when the parent attempts to release the childViewOfMemoryNot released.MemoryLeakage, reportMemoryWarn the program to crash.
CauseUIview memoryThe reason for the leak is that when releasedUIView, Its childViewThe occupied resources are not released. If the animation is not finishedMemoryWill not be released.
The following code demonstrates the above process:
Add an AnimationView to the main view, and then add a view in the AnimationView:
- Animations *animationView=[[Animations alloc] initWithFrame:CGRectMake(10, 10, 270, 400)];
- [animationView setBackgroundColor:[UIColor yellowColor]];
- [self addSubview:animationView];
- [animationView release];
Add a recursive animation to the Animations View:
- - (void)wobble {
- NSLog(@">>>>>>>>>>>>>>>>>>");
- CGFloat rotation = (kWobbleRadians * M_PI * 2) / 360.0;
- CGAffineTransform wobbleLeft = CGAffineTransformMakeRotation(rotation);
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:kWobbleTime];
- [UIView setAnimationDelegate:self];
- self.transform = wobbleLeft;
- // if (!releaseFlage) {
- [UIView setAnimationDidStopSelector:@selector(wobble)];
- // }
- [UIView setAnimationRepeatAutoreverses:NO];
- [UIView commitAnimations];
- }
Because the current class needs to execute recursion once every three seconds, when the AnimationView is removed from the main view, the current view tree will not be released because the animation of the subview of the AnimationView is a recursion that has not ended, but it is not displayed on the interface, resulting in Memory leakage. The solution is as follows. When the AnimationView is to be removed, you need to remove the animation from its subview. My approach is to add the following method to the AnimationView:
- - (void)willRemoveSubview:(UIView *)subview
- {
- Animations *downBookView=(Animations *)subview;
- downBookView.releaseFlage=TRUE;
- }
This wayMemoryItsViewThe tree will be released.MemoryAnd is completely released from the system.
Summary:Objective-CStudy NotesUIView memoryI hope this article will help you with the release issue.