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
1
vote
2 answers
swift - retain cycle when calling inner function?
Do I have a retain cycle when inner function progressComplete is calling inside closure without weak self and inside of it (progressComplete) i use self?
class SomeClass{
var manager = Manager()
var someVar:Int?
func…

Asi Givati
- 1,348
- 1
- 15
- 31
1
vote
0 answers
Do closures inside collectionView(_:cellForItemAt:) need weak self?
Edit: I checked the similar question, but that's passing self:
var buttonAction: ((UITableViewCell) -> Void)?
@IBAction func buttonPressed(_ sender: Any) {
buttonAction?(self)
}
While I'm no passing anything
I have a UICollectionView full of…

aheze
- 24,434
- 8
- 68
- 125
1
vote
3 answers
Is weak self needed for table view cell button closure
In trying to avoid retain cycles, would using [weak self] in in a UITableViewCell button action be necessary? Example:
in ViewController's cellForRow
cell.buttonAction = { (cell) [weak self] in
self.someFunction()
}
in TableViewCell class
var…

Noah Iarrobino
- 1,435
- 1
- 10
- 31
1
vote
1 answer
Should I use `weak self` when making asynchronous network request?
Here is my method to fetch some data from the network:
func fetchProducts(parameters: [String: Any],
success: @escaping ([Product]) -> Void)
As you noticed, it has escaping closure. Here is how I call above method in…

neo
- 1,314
- 2
- 14
- 34
1
vote
2 answers
Am I capturing self in this nested function? The compiler does not fire a warning
I can't find any official documentation on this and there's mixed opinions out there.
In the following situation, all is well.
final class MyVC: UIViewController {
var space: Space!
private let tableView =…

bobby123uk
- 892
- 4
- 17
1
vote
2 answers
Swift: Assign a class method to a handler and handling weak self
I just went memory-leak hunting in the app I am working on, and noticed that the following produces a memory leak:
class SubClass {
var didCloseHandler: (() -> Void)?
}
class MainClass {
var subClass = SubClass()
func setup {
…

BlackWolf
- 5,239
- 5
- 33
- 60
1
vote
1 answer
Memory leak when injecting class instead of instance?
Am I susceptible to memory leaks if I inject dependencies as classes and not as instances? The reason I want to do this is that the 3rd-party library I am integrating only exposes class methods.
Is it dangerous if I do just that? (and why and when…

Apostolos Apostolidis
- 179
- 4
- 16
1
vote
2 answers
Function inside Function retain cycle
I was wondering how to avoid retain cycle in the following scenario:
private func setupDismissCallbacks() {
// inner func
func dismiss() {
self.videoExporter?.cancel()
self.rootViewController.dismiss(animated: true, completion:…

Roi Mulia
- 5,626
- 11
- 54
- 105
1
vote
1 answer
Local Object became nil before returning completion block - Objective C
I have an object that declared inside a function. this object makes a function call which has a completion block. the function executes properly.
This function makes a network call (which is in another class). after getting the result from network…

Musadhikh Muhammed K
- 375
- 2
- 16
1
vote
1 answer
Memory is not released during perform batch updates in CollectionView
I've been working on a project of using two UICollectionView's I have on that is the MainViewController with full screen cells that scroll horizontally and one that sits on that full screen cell and scrolls vertically. I have a functionality that…

TheRedCamaro3.0 3.0
- 781
- 6
- 26
1
vote
1 answer
RxSwfit bind operation retain cycle
I am new at rxswift framework.I have written a code below and I am not sure.Is there any retain cycle? Must I use weak reference to self?
loginButton.rx.tap.bind {
print(self.nameText.value ?? "")
self.nameText.accept("ahmet vefa…

ahmetvefa53
- 581
- 6
- 20
1
vote
2 answers
Swift Retain Cycle Issue when setting via instance method
If I create a class Agent like this. which holds a weak reference to another Agent object.
class Agent {
weak var partner: Agent?
var name: String
init(name: String) {
self.name = name
}
func makePartner(_ agent:…

Ralph Korvin
- 55
- 6
1
vote
0 answers
IOS UIButton retain cycle
I have a button on a VC as a subview of my root view.
The VC is loaded from a storyboard.
I get this very weird retain cycle:
Can anyone assist?

YanivH
- 539
- 4
- 18
1
vote
1 answer
Swift iOS -storyboard.instantiateViewController(withIdentifier:) causing retain cycle
I'm using my physical device and not the simulator.
I'm instantiating a vc using storyboard.instantiateViewController(withIdentifier:) and presenting it modally. I dismiss it using presentingViewController?.dismiss(animated: true, completion: nil).…

Lance Samaria
- 17,576
- 18
- 108
- 256
1
vote
1 answer
What is the difference between retain cycle in these closures? Standard closure vs object initialized closure
I understand the typical retain cycle with a closure when referencing self within the closure, but is there a reason the label does not produce a retain cycle? I've tested commenting the testClosure out vs commenting the label out and only the…

jo1717a
- 11
- 1