Why does ios leave dealloc or cause Memory Leak in UIViewController,

Source: Internet
Author: User

Why does ios leave dealloc or cause Memory Leak in UIViewController,

Since MyLeaksFinder is imported into the project, Memory Leak prompts are often reported in the project. After several debugging times, several possible causes of this situation are summarized:

1. The NSTimer timer is used in the VC. After the VC pushes -- pop, if the NSTimer object is not destroyed, the memory leakage will occur, that is, the current VC is referenced, as a result, the reference count of the controller is increased by 1. If the nstloc is not destroyed, the Controller keeps the VC and cannot be released. Therefore, the dealloc method is not called. Therefore, you need to destroy the NSTimer used by the Controller before viewWillDisappear.

Destruction method: [timer invalidate]; // destroy timer

Timer = nil; // set nil

Or the destroy method in GCD:

Dispatch_source_cancel (_ timer );

2. Strong reference self is used in the Block code Block, which is easily caused by loop reference and the object cannot be released for a long time.

You can:

# Define WS (weakSelf )? _ Weak _ typeof (self) weakSelf = self;

Use Weak reference weakSelf to replace self,

Or when the project calls internal member variables and attributes in the callback code block, self. name or _ name will cause circular reference. The method is the same as above.

3. the proxy method attribute used in the project VC is not the weak attribute. The specific method is as follows:

: @ Property (nonatomic, weak) id delegate;

The specific causes are still being explored. If there are any new causes, you are welcome to add them in the comment area. I wish you a bright future!

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.