nsproxy resolves cadisplaylink, nstimer problem caused by strong reference to target

Source: Internet
Author: User

Recent development encountered a problem, in the cell opened the Cadisplaylink and joined the Runloop, in the release, VC and other cell dealloc are gone, only open the timer cell does not go Dealloc method.

The view found that nstimer against Target was a strong reference to the target leak, to resolve this problem must break the circular reference, in the middle of Nstimer and target more than one layer of proxy (proxy), agent as target by Nstimer strong reference, It also weakly references the real target and forwards the message to it.

Nsproxy is early to understand but not really used, this time just practice.

Before using Nsproxy, simply review the runtime message forwarding mechanism:

The first step, dynamic method parsing

+ (BOOL) Resolveinstancemethod: (SEL) sel;

+ (BOOL) Resolveclassmethod: (SEL) sel;

Second step, standby receiver

-(ID) Forwardingtargetforselector: (SEL) Selector

If an object implements this method and returns a nil result, the object is used as the new recipient of the message, and the message is distributed to the object. Of course this object cannot be self itself, otherwise there is an infinite loop. Of course, if we do not specify an object to handle aselector, we should call the implementation of the parent class to return the result.


Step three, full message forwarding

-(void) Forwardinvocation: (nsinvocation *) invocation;

In this step, the runtime system gives the message receiver the last chance to forward the message to another object. Object creates a Nsinvocation object that represents the message, encapsulating all the details about the message that have not been processed in the aninvocation, including selector, Target (target), and parameters. We can choose to forward messages to other objects in the Forwardinvocation method.


-(Nullable Nsmethodsignature *) Methodsignatureforselector: (SEL) The SEL message forwarding mechanism uses the information obtained from this method to create the Nsinvocation object. So we have to rewrite this method to provide a proper method signature for the given selector.


Our proxy is in the second step, the target as the message receiver response method. The specific code can refer to yy great God's Yyweakproxy

Https://github.com/yehot/YYFPSLabel/blob/master/YYFPSLabel/YYFPSLabel/YYWeakProxy.m


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.