A retain cycle is a situation in reference-counted memory management when two (or sometimes more) objects have strong references to each other. Normally, objects are destroyed when their reference count reaches zero, and the references they hold are released at that time. In a cycle, each object keeps the other alive, and neither will be destroyed unless the cycle is deliberately broken.
Questions tagged [retain-cycle]
301 questions
13
votes
3 answers
Retain cycle between class and struct
Assuming I have following code:
struct X {
let propertyOfTypeY: Y
}
class Y {
var propertyOfTypeX: X?
}
let y = Y()
let x = X(propertyOfTypeY: y)
y.propertyOfTypeX = x
If these were both classes, then it would mean a retain cycle. However…

user3581248
- 964
- 10
- 22
12
votes
4 answers
Should I use weakSelf in nested blocks?
I'm trying to correctly avoid retain cycles with blocks in Objective C, and am not sure about having nested blocks.
If I write a simple block like this:
[self doSomethingWithBlock:^{
[self doSomethingElse];
}];
The compiler catches and warns me…

SaltyNuts
- 5,068
- 8
- 48
- 80
12
votes
1 answer
How to fix "Capturing 'block' strongly in this block is likely to lead to a retain cycle"
I am working on this code, which does some lengthy asyncronous operation on the net and when it finishes it triggers a completion block where some test is executed and if a variable get a certain value another lengthy operation should start…

nico9T
- 2,496
- 2
- 26
- 44
11
votes
1 answer
Retain Cycle in Swift delegate
I have a UIViewController and in it a UIToolbar. They get instantiated from a storyboard.
I made a custom class for my UIToolbar. Based on some logic I do or do not show buttons on it.
The UIViewController needs to take action when some of the…

Bocaxica
- 3,911
- 3
- 37
- 54
10
votes
3 answers
Print strong owners of an object , Swift
There are some retain cycle in my iOS application.
For a particular viewController, stuck in a retain cycle, I have tried making all delegates weak. But when I simulate memory warning from simulator , didRecieveMemoryWarning is called , but deinit…

ila
- 920
- 12
- 35
10
votes
1 answer
Retain cycle when grabing values or keys from Dictionary in Swift
When I grab values from a Dictionary and put them into Array, I can't release memory any more. I tried to remove all object from Array and Dictionary, but these object still exist somewhere (deinit were not called).
I was playing in the following…

Matjaz
- 113
- 6
10
votes
1 answer
Does adding a KVO observer to self cause a memory leak?
In a nsobject you have a property "keyPath" you want to observe itself and you use
[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:nil];
Does the above line cause a retain cycle?
I present this question…

Yogurt
- 2,913
- 2
- 32
- 63
10
votes
2 answers
Why there is no retain loop between UINavigationController and UIViewControllers
Situation: there is UINavigationController with pushed UIViewController.
1.UIViewController has strong reference to UINavigationController
@property(nonatomic,readonly,retain) UINavigationController *navigationController
2.UINavigationController…

Avt
- 16,927
- 4
- 52
- 72
9
votes
1 answer
Knowing where retain cycles are and removing them
I was wondering if there was an easy way (or at least a way) to find out where retain cycles exist in your program. Also, if I then know where these retain cycles exist, depending on their types (e.g. variable or closure), how do I make them weak. I…

J.Treutlein
- 963
- 8
- 23
9
votes
3 answers
Retain Cycle in ARC
I have never worked on non ARC based project. I just came across a zombie on my ARC based project. I found it was because of retain cycle.I am just wondering what is a retain cycle.Can
Could you give me an example for retain cycle?

Raj
- 1,119
- 1
- 15
- 32
8
votes
1 answer
What happens to Dispatch Queues when UIViewController is Deallocated?
I am trying to better understand retain cycles, especially relative to Dispatch Queues. I am working with AVFoundation and managing an AVCaptureSession on a sessionQueue:
private let sessionQueue = DispatchQueue(label:…

andypf
- 197
- 2
- 10
8
votes
2 answers
How to avoid a retain cycle when using an array of delegates in Swift
In one of my classes I use an array of delegates (the class is a singleton). This is causing an retain cycle. I know I can avoid the retain cycle when I use only one delegate by making the delegate weak. But this is not working for my array of…

Leontien
- 612
- 5
- 22
8
votes
3 answers
ARC, self and blocks
I thought I understood the usage of self in a block that is copied is a no no.
But in an attempt to clean my code i enabled a bunch of warnings in Xcode, one called "Sending messages to weak pointers"
so now in all my blocks, every time I use my…

cream-corn
- 1,820
- 14
- 26
7
votes
3 answers
Why does UIAlertController create a retain cycle with self?
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"alert" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"action" style:UIAlertActionStyleDefault…

trapper
- 11,716
- 7
- 38
- 82
7
votes
3 answers
Swift - Expecting a leak after strongly capturing self in closure
Can anyone please explain why this doesn't leak?
I'm capturing self within a closure so I would have two strong pointers pointing at each other, therefore, the deinit message shouldn't ever be called for the Person object.
First, this is my class…

RGML
- 3,429
- 2
- 22
- 18