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
7
votes
2 answers
What is the reason of @strongify
In ReactiveCocoa there is macro to prevent retain cycle @weakify and @strongify. From my understanding @weakify do something like what I usually do that is create __weak reference for using in the block, but what about @strongify?
Why do I need to…

sarunw
- 8,036
- 11
- 48
- 84
7
votes
1 answer
Swift Memory Management: Storing func in var
I'm looking for the best practice for storing functions as variable in other objects. Specifically, I'm looking to avoid retain cycles inherent in capturing self in the function.
Coming from objective-c and blocks, I would normally do something…

Aaron Hayman
- 8,492
- 2
- 36
- 63
6
votes
2 answers
Does using a function as a closure retain self?
I'm having trouble tracking down a retain cycle. I think it's to do with the way I subscribe to events. Pseudo code is like this:
override func viewDidLoad() {
func handleEvent() {
self.doSomething()
}
subscribe("eventName", block:…

Roland Rabien
- 8,750
- 7
- 50
- 67
6
votes
1 answer
SpriteKit not deallocating all used memory
I have ready many (if not all) articles on SO and other sites about the disasters of dealing with SpriteKit and memory issues. My problem, as many others have had, is after i leave my SpriteKit scene barely any of the memory added during the scene…

TheValyreanGroup
- 3,554
- 2
- 12
- 30
6
votes
2 answers
Unowned self in a closure in a closure
If I have a closure in another closure is it enough to use unowned/weak once in the outer closure to avoid retain cycles?
Example:
foo.aClosure({[unowned self] (allowed: Bool) in
if allowed {
self.doStuff()
…

lukas_o
- 3,776
- 4
- 34
- 50
6
votes
3 answers
Why is the capture specifier optional in capture lists?
There seems to be a curious syntax glitch in the capture list syntax in Swift. If I declare multiple captured variables, the capture specifier only applies to the first one:
let closure = { [unowned x, y] in … }
Now I would expect y to be unowned,…

zoul
- 102,279
- 44
- 260
- 354
6
votes
2 answers
Does passing a function belonging to self cause a retain cycle when not in a closure?
If you need to reference self inside of a closure, it's good practice to pass it in as weak or unowned to prevent a retain cycle.
If I pass the function that belongs to self directly, will it cause a retain cycle? Or does it need to be nested…

Chris
- 7,270
- 19
- 66
- 110
6
votes
1 answer
Should I use weak self inside a computed property's closure?
I am confused about using self inside a closure.
When should we declare [weak self] ? An obvious case that I understand is
class Foo{
var closure: ( Void -> Void )?
var x = 0
func doSomething(){
closure = { [weak self] in
…

nRewik
- 8,958
- 4
- 23
- 30
6
votes
1 answer
Swift Retain Cycles and Closures
I have tried to do a lot of research on understanding retain cycles. I can't seem to find anything on my examples though. I do know that if i set a property to a closure then a retain cycle happens and need to use weak or unowned. But i have 2…

smitt04
- 3,018
- 2
- 28
- 30
6
votes
2 answers
Purposely Create Retain Cycle (Objective C without GC)
Is there ever a case where purposely creating a retain cycle to prevent deallocation, then cleaning it up later, is the best solution to a problem?
If so, are there examples of this in the Cocoa Touch or NextStep frameworks?
I intend this question…

MikeS
- 3,891
- 6
- 35
- 51
5
votes
1 answer
SwiftUI UIViewRepresentable PDFKit PDFView AttributeGraph: cycle detected through attribute
Edit:
Created a sample project illustrating the issue:
https://github.com/Harold-D/PDFView_Representable
Question:
I'm at a lost, I have this very simple UIViewRepresentable wrapper around PDFView in SwiftUI
import PDFKit
import SwiftUI
struct…

DIJ
- 347
- 4
- 19
5
votes
2 answers
Is [Weak self] always needed when dealing with URLSession?
I can't figure out if I need to use [weak self] in this situation or not ?
HTTPClient.swift:
struct HTTPClient {
let session = URLSession.shared
func get(url: URL, completion: @escaping (Data) -> Void) {
session.dataTask(with:…

iOSGeek
- 5,115
- 9
- 46
- 74
5
votes
1 answer
Closures and static funcs
I have a ViewModel class with a method like this:
func getUserSettings() {
UserSettingsManager.getInfo { (result, error) in
if error == nil {
self.userData = result
}
}
}
This class viewModel is instantiated and then…

AppsDev
- 12,319
- 23
- 93
- 186
5
votes
2 answers
Should I Use "weakSelf" In a dispatch Block?
I've heard that I should always use weakSelf in blocks to avoid retain cycles, but what about dispatch blocks? In this case, my method handles an error response from my server in the following code:
//handling server errors (particularly "Token…

Rafi
- 1,902
- 3
- 24
- 46
5
votes
5 answers
Why specify [unowned self] in blocks where you depend on self being there?
I want self to be non-nil and I'm sure it will be, during the blocks execution. So why explicitly specify [unowned self] ?
object.executeBlock {
date = self.lastModified
}
vs
object.executeBlock { [unowned self] in
date =…
user1951992