Depth: Memory leakage caused by ARC and leakage caused by deep arc

Source: Internet
Author: User

Depth: Memory leakage caused by ARC and leakage caused by deep arc

IOS provides the ARC function, which greatly simplifies the code for memory management.

However, using ARC does not mean that there will be no memory leakage. If used improperly, the memory leakage will still occur.

The following lists two memory leaks.

1. cycle reference

A has an attribute that references B and B has an attribute that references A. If both are strong references, both objects cannot be released.

This problem often occurs when delegate is declared as the strong attribute.

For example,

@ Interface SampleViewController

@ Property (nonatomic, strong) SampleClass * sampleClass;

@ End

@ Interface SampleClass

@ Property (nonatomic, strong) SampleViewController * delegate;

@ End

 

In the above example, the solution is to change the strong of the delegate attribute of SampleClass to assing.

 

2. endless loop

If an infinite loop exists in a ViewController, The ViewController cannot be released even if the view corresponding to the ViewController is disabled.

This problem often occurs in animation processing.

For example,

For example,

CATransition * transition = [CATransition animation];

Transition. duration = 0.5;

Tansition. repeatCount = HUGE_VALL;

[Self. view. layer addAnimation: transition forKey: "myAnimation"];

 

In the preceding example, the number of times the animation repeats is set to HUGE_VALL. A large value is basically equivalent to an infinite loop.

The solution is to stop the animation when the ViewController is disabled.

-(Void) viewWillDisappear :( BOOL) animated {

[Self. view. layer removeAllAnimations];

}

 

Of course, there are more than two types of memory leaks.

That is, when using ARC, we also need to have a deep understanding of the memory management mechanism of iOS so as to effectively avoid Memory leakage.


What should I do if there is a memory leak in the arc program?

Using arc and non-arc hybrid encoding, non-arc classes are instantiated and used in arc, and memory leakage occurs in arc, and it should be arc, so release cannot be used, autorelease and dealloc manage the memory. Do you have this problem? Is there any solution?
This should not happen to you. If a class is ARC, it should follow the usage of ARC in this class without worrying about whether the class is ARC. Similarly, in a non-ARC class, the memory management principle must be followed.
With ARC, the compiler helps you manage when to release, retain, and you need to manage it by yourself without using ARC. In the end, it's just a matter of who manages it, so let's take a look, the problem may not be related to ARC.
If the problem cannot be found, we recommend that you find the leaked object and assign it to nil, because in ARC, once the object has no pointer to it, it will be released immediately.
 
How to solve Memory leakage detected in the arc environment

Is this file in the arc environment? Is it special. View Original post>

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.