0

Let's say you have have a viewController with:

@property (strong) object* A 
@property (strong) object* B

You then purposely create a retain cycle at somepoint, without timers, such that

self.A.someStrongProperty = self  //retain cycle

Question: Suppose the VC containing these properties gets deallocated, could a retain cycle or memory leak persist?

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Emin Israfil iOS
  • 1,801
  • 1
  • 17
  • 26

2 Answers2

1

In the code you have posted above, there is no retain cycle.

A retain cycle would be self.A = self; or more likely, self.A.someStrongProperty = self.

Edit: In the case you have edited above, assuming self is a view controller, it would not deallocate because of the retain cycle. You should change your someStrongProperty to be a weak property, which will prevent the retain cycle.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
0

Yes in case you retain self you are causing a retain cycle.

This will cause the self instance not to be deallocated, causing a memory leak.

To prevent this, you can either use a weak property or manually setting someStrongProperty to nil at some point, in order to break the retain cycle.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • `-setA:` will strongly retain `B` while assigning `_A = B`, which is not an issue. – Gabriele Petronella Oct 29 '13 at 21:14
  • 2
    First of all, this is ARC and `_A = B` does retain `B`. Second, `A` is a pointer to an object `*A`, and `B` is a pointer to an object `*B`. All `self.A = self.B` does is release `*A` and retain `*B`. – Kevin Oct 29 '13 at 21:17
  • I'm talking about the setter implementation when referring to `_A = B`. What will happen is something like `[_A release]; _A = [self.B retain];` which will cause `B` to be sent a `retain` message. – Gabriele Petronella Oct 29 '13 at 21:19
  • So yes, we are saying the same thing. `A` will be holding a strong reference to `*B`, but oviously no cycle is created. – Gabriele Petronella Oct 29 '13 at 21:23
  • Yes, A becomes a strong reference to B. I thought you were saying the object originally at A retained the object at B during that assignment. Glad we cleared that up. – Kevin Oct 29 '13 at 21:37
  • Absolutely not, my bad. Poor choice of terms indeed, I'm glad too. – Gabriele Petronella Oct 29 '13 at 21:48