Project Issues Summary: block memory leaks and Nstimer usage issues

Source: Internet
Author: User

memory leaks in block

In our code about the use of blocks can be said everywhere, the first time to contact the block is about the UIView animation, then feel that the use of block magic, and then the analysis of the block is actually a C language function, but we can call this function at any place. With this understanding, I began to use block frequently. After doing the project found that the use of block unexpectedly will cause memory leaks, and began to debug their own research block memory management problems.

Normal block use (including block animation)

Here is a simple block to use, in which we can add any of the actions we want to do, most of the use is also

1 void (^block) (int) = ^ (int  num) {2    /// Here you can add additional code .... 3    NSLog (@ "int number is%d", num); 4 };

The block animations that include UIView are also used, where we define the location of an image view and the change in transparency

1 [UIView animatewithduration:2.0 animations:^ (void) {2     Smallimage.frame = CGRectMake (in); 3 } completion:^(BOOL finished) {4      0;

In these block operations, I always thought that the objects in the block would be released after the block was used (but UIView's operation seemed to do so)

Block Memory Management First

Until I encountered a situation in the project: I set up 2 controllers first and second, where second contains a block object, and the implementation of block is implemented in first (the normal block pass value does this). The interface is pushed out of the second controller by the first controller. But when I second controller pop, the problem occurs second controller does not go Delloc method, that is, after the pop second controller has not been destroyed (because at that time to do some operations in Delloc, only to find this problem)! The block sample code is as follows:

Implementation of block in first controller

1 Secondviewcontroller *SECONDVC = [[Secondviewcontroller alloc] init]; 2 secondvc. Block = ^ (NSString *text) {3         self.text = secondvc.text; 4 };

Such a simple value block use, incredibly can cause the second controller can not be released, so study its principle, and online search data, draw a conclusion: The second controller is held in block in order to cause it cannot be released. Because block is essentially a function, and the compiler doesn't know when you're going to call the value inside the block, in order to make sure that the SECONDVC in the compiler won't be released, The compiler automatically holds it once (using the Block method in its own class to manipulate its own member properties also causes its own reference to be calculated by 1, causing it to fail to be freed).

The solution is simply to add a weak reference object externally to the object that needs to be manipulated in the block, that is, the __weak typeof (object name) Alias = object name;

1 Secondviewcontroller *SECONDVC = [[Secondviewcontroller alloc] init]; 2 typeof (SECONDVC) Second = SECONDVC; 3 typeof (self) VC = self ; 4 2 secondvc. Block = ^ (NSString *text) {53         vc.text = second.text; 6 4 };

This will effectively prevent memory leaks caused by block usage.

Nstimer Usage Issues

In addition, I met a question about the use of Nstimer in the project. We want to stop the Nstimer and set it to nil while the controller is destroyed.

1 -(void) dealloc {2    [Self.timer invalidate]; 3     Self.timer = Nil; 4     NSLog (@ "%@ dealloc"class])); 5 }

However, after the controller was pop and did not go this method (again memory leak), because of the block memory leaks before the problem, I think it is because this _timer load when the self has been held

_timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (Timerup:) userinfo:nil Repeats:yes];

The debugging test, sure enough, was a problem here because it was held on the controller once. So I thought that since then I would simply make a judgment in Viewwilldisappear (), if it is a pop controller, I will first set [Self.timer invalidate] operation so that the controller will go Dealloc () method. Later on the Internet to find the data found a simpler solution, that is, the same as the memory management of the block using weak reference objects

1 typeof (self) VC = self ; 2 _timer = [Nstimer scheduledtimerwithtimeinterval:1.0 TARGET:VC selector: @selector (Timerup:) Userinfo:nil Repeats:yes];

Such a solution will be more simple than my previous, the only thing to note is the scope of the VC here!

Project Issues Summary: block memory leaks and Nstimer usage issues

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.