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
0
votes
0 answers
Assigning a singleton to a variable (Swift)
Is it an anti-pattern to have the following code?
I want to have the singleton (ClassB) as a variable so that I have some sort of dependency injection while still using a singleton. But I have been hinted that without having mySingleton as weak I…

Apostolos Apostolidis
- 179
- 4
- 16
0
votes
1 answer
Unowned necessary for type in array?
In the following code, is it necessary to have unowned if the Swift array is passed by value ?
Category has a property for a Swift array, not an Item, so unowned is not necessary right?
final class Item: Base {
unowned let category: Category
…

Jonesy
- 195
- 8
0
votes
1 answer
Is it safe to have static closures in structs?
I'm getting used to structs over classes in Swift, but have a concern about best practices if I'm possibly generating retain cycles due to closures not having [unowned self] or [weak self] ? (which isn't allowed in a struct or protocol). And the…

Sebastian Dwornik
- 2,526
- 2
- 34
- 57
0
votes
1 answer
ViewController not released from Memory
I have this view hierarchy
RouterDashboardViewController : RootViewController
RootViewController : UIViewController
Currently, RouterDashboardViewController instance is in the navigation stack. When I reset rootViewController of…

Sumit Jangra
- 651
- 5
- 16
0
votes
1 answer
Memory Leak NSBlockOperation
I declared NSBlockOperation with an object declared inside that operation. My app constantly is crashing because of memory issue. Appreciate any hint with a great explanation on this spent several hours still no success.
runtime: Memory Issues - (5…

Atalyk
- 495
- 1
- 5
- 11
0
votes
1 answer
weakself inside dispatch_async queue(dispatch_get_main_queue(), ^{})
Below code snippet is in objective C
__weak MyView *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.activityIndicatorView stopAnimating];
[weakSelf.activityIndicatorView removeFromSuperview];
…

srus2017
- 394
- 2
- 14
0
votes
1 answer
Delegate-Class Never Released
I have the problem, that my delegate-class is never reinitialised if I pass it as delegate to NSURLSession:
// Playground-compatible
import Foundation
class Downloader: NSObject, URLSessionDataDelegate {
private var session: URLSession! = nil
…

K. Biermann
- 1,295
- 10
- 22
0
votes
2 answers
Creating AVPlayerLayer prevents releasing AVPlayer
If I ever set an AVPlayerLayer, then there will be some retain cycle that will prevent deinit from ever being called.
import AVFoundation
class MyPlayer: AVPlayer {
fileprivate(set) lazy var playerLayer: AVPlayerLayer = {
// Create a…

Cœur
- 37,241
- 25
- 195
- 267
0
votes
3 answers
Deallocate view controllers in navigation controller that have a reference to self
Say I have view controllers A, B, C, D & E all embedded in a navigation controller. In view controller B, I have a custom UIImageView object. In C, I have a custom UITextfield object. Both custom classes have a reference to the view controller for…

user7804097
- 308
- 1
- 3
- 17
0
votes
2 answers
Swift Retain Cycles
In my iOS app I have
class Node {
var value: String
var isExpanded: Bool
var children: [Node] = []
private var flattenElementsCache: [Node]!
// init methods
var flattenElements: [Node] {
if let cache =…

Giorgio
- 1,973
- 4
- 36
- 51
0
votes
2 answers
How much memory is allocated to UIViewController variables
Let's say I have a class Animal
class Animal: NSObject {
var name: String = ""
var weight: Double = 0
}
In my view controller #1, I have an array of these objects:
class ViewController1: UIViewController {
var animals: [Animal] = [ .... ]
…

7ball
- 2,183
- 4
- 26
- 61
0
votes
1 answer
Use closure instead selector argument for UIBarButtonItem BUT without using weak self
In order to use closure in argument of UIBarButtonItem I am using a subclass:
class ActionBarButtonItem: UIBarButtonItem {
private var actionHandler: (() -> Void)?
convenience init(title: String?, style: UIBarButtonItemStyle, actionHandler:…

iOSGeek
- 5,115
- 9
- 46
- 74
0
votes
0 answers
Swift Closure retain cycles
I'd like to know if the following counts as a retained cycle:
class MyViewController: UIViewController {
var foo: Int = 3
func fooBar() {
let closure = {(x: Int) in
print(x > self.foo)
}
closure(100) // Does this cause a…

7ball
- 2,183
- 4
- 26
- 61
0
votes
1 answer
xcode retain cycle not show in memory graph
I'm trying to understand how xcode debugging tool works in terms of detecting retain cycles.
I have a simple Parent and Child view controllers both holds references to each other.
And after executing app opening closing VC several time, when I open…

mihatel
- 846
- 14
- 34
0
votes
1 answer
Do functions cause retain cycles?
I know that using a closure will always capture the properties or methods that are used inside the closure.
But do functions cause retain cycles too? If it does please explain it to me!
If not, then why do we even use closures if we are risking a…

darren zou
- 41
- 5