简体   繁体   中英

Retain cycle Objective C with 3 objects

I have 3 objects - a ViewController, another VC that is the strong property of the ViewController (call it VC2), and a class called "Checker" that checks to see if an action occurred in VC2. "Checker" is just a class with class methods - no instance methods.

When an action occurs in VC2, it sends an data structure to "Checker" which processes it. I need to send that processed data back to ViewController. I grab the instance of ViewController and send it via method. ViewController takes the processed data and displays it in it's view.

Will this create a retain cycle or any other memory-related problems?

In respect of your comments, the A is:

Grabbing the already existing object reference ("already existing instance") does not create a retain cycle. Passing a reference to a method does not cause a strong reference by default:

{
  …
  [Checker doSomethingWith:vc2.referenceToVC1]; // Passing does not retain
  …
}

However, even it would or you copy the instance reference into a strong local var before passing it, the ownership would be a short-timer and resolved automatically by ARC.

{
  …
  VC1 vc1 = vc2.referenceToVC1; // strong reference causing ownership, retain
  [Checker doSomethingWith:vc1];
  …
  // vc1 and its strong reference is automatically given up, release 
}

This is, because the local var (and potentially the parameter var, which is akin of local var) are additional references, but since nothing is referring them, it is no retain cycle .

You do not have a retain cycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM