Emin Israfil iOS
Emin Israfil iOS

Reputation: 1811

iOS: Simple Retain Cycle

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?

Upvotes: 0

Views: 839

Answers (2)

Gabriele Petronella
Gabriele Petronella

Reputation: 108121

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.

Upvotes: 0

Léo Natan
Léo Natan

Reputation: 57050

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.

Upvotes: 1

Related Questions