Objective-C Study Notes UIView memory release Problems

Source: Internet
Author: User

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:

 
 
  1. Animations *animationView=[[Animations alloc] initWithFrame:CGRectMake(10, 10, 270, 400)];   
  2.         [animationView setBackgroundColor:[UIColor yellowColor]];   
  3.         [self addSubview:animationView];   
  4.         [animationView release]; 

Add a recursive animation to the Animations View:

 
 
  1. - (void)wobble {   
  2.     NSLog(@">>>>>>>>>>>>>>>>>>");   
  3.         CGFloat rotation = (kWobbleRadians * M_PI * 2) / 360.0;   
  4.         CGAffineTransform wobbleLeft = CGAffineTransformMakeRotation(rotation);   
  5.         [UIView beginAnimations:nil context:nil];   
  6.         [UIView setAnimationDuration:kWobbleTime];   
  7.         [UIView setAnimationDelegate:self];   
  8.         self.transform = wobbleLeft;   
  9.    //     if (!releaseFlage) {   
  10.             [UIView setAnimationDidStopSelector:@selector(wobble)];   
  11.    //     }   
  12.         [UIView setAnimationRepeatAutoreverses:NO];   
  13.         [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:

 
 
  1. - (void)willRemoveSubview:(UIView *)subview   
  2. {   
  3.     Animations *downBookView=(Animations *)subview;   
  4.     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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.