Release issues for controllers in iOS

Source: Internet
Author: User

ARC Engineering can override the Dealloc method and be called by the system, but do not need to manually call the parent class Dealloc, the handwritten [super Dealloc] method will error, in fact, the system will automatically help you call the parent class Dealloc method, do not need you to implement. You can see if the controller is freed by printing the log in the Dealloc method.

The controller is released after being moved out of the stack by the pop, but sometimes it is found that the controller does not call the Dealloc method when it is out of the stack, in the final analysis, because the current controller is strongly referenced by an object, the controller reference count is not 0, the system can not help you to release this part of memory.

Nstimer is not destroyed in the controller

When there is a nstimer in the controller, you need to be aware, because when [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (updatetime:) Userinfo:nil Repeats:yes]; When this/target:self/increases the retarncountr of the VC, if you don't invalidate the timer, you don't want to call Dealloc. Need to nstimer the controller to be destroyed before viewwilldisappear.

    • [Timer invalidate]; Destroy timer

    • Timer = nil; Place Nil

The agent in the controller is not a weak property

For example @property (nonatomic, weak) idIf the Proxy property is set to strong, it means that delegate also has a strong reference to the view controller, which can cause circular references. Causes the controller to not be released and eventually causes a memory leak.

Circular reference of block in controller

The block will make a strong reference to all objects inside it (under ARC)/PS:MRC will retain plus 1/, including the current controller self, so there may be problems with circular references.
That is, an object has a block property, but the block attribute also refers to the other member variables of the object, then the variable itself will be strong application, then the object itself and his own block property formed a circular reference. Under Arc it needs to be modified like this: (/That is, to generate a weak reference to its own object /)

    • __weak typeof (self) weakself = self;

That is, for the sake of insurance, all of the blocks involved in self are replaced with weakself.

Release issues for controllers in iOS

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.