Questions tagged [retain-cycle]

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.

0
votes
0answers
57 views

Why is `self` is not required in first closure?

1) I cannot understand why is self not required in first closure. Usually you get a compile-time error that goes something like this: "add self to make capture semantics explicit". myCollectionView is ...
0
votes
0answers
45 views

Do I need to capture self weakly inside a UIAlertController's action completion handler

I've read that self doesn't need to be captured weakly in a UIAlertController action completion handler if the UIAlertController reference is weak. The reason being that UIAlertController is designed ...
0
votes
1answer
54 views

ViewController in navigation stack won't deinit when popped, causing memory leak

In my app I have a RootViewController which all my ViewControllers are subclassed from. When developing, I usually use this in my RootVC: deinit { print("\(type(of: self)) deinit") } so that I ...
0
votes
0answers
59 views

Swift 4 retain cycle different from swift 3? [duplicate]

I incurred a small problem to solve retain cycle in Swift 4. (the same code works fine in swift 3) what do i miss please? class User { weak var device:Device? init() { print("User ...
0
votes
1answer
41 views

Adding a collectionview to a collection view cell leads to memory growing

Ive created a CollectionView called and on each CollectionViewCell I have created a CollectionView. Below I have included the code for the collectionView which goes inside the cell of the ...
0
votes
1answer
121 views

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 ...
2
votes
2answers
25 views

why UIView variables could not create retain cycle problem?

When I searched code of UIView , I could not understand that why superview and subviews properities does not create retain cycle? extension UIView { open var superview: UIView? { get } open var ...
0
votes
1answer
73 views

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....
0
votes
0answers
42 views

I seem to have a retain cycle with a weak delegate

I'm using a networker inside of a view controller. The networker has a weak delegate reference to the view controller. After a view controller is dismissed I kick off a timer to check whether the vc ...
3
votes
2answers
119 views

How to investigate into memory leaks in ios?

I have created a simple flow to test memory in ios app. I have two view controllers in a navigation stack. I am showing an alert in the first view controller to allow user to move to the next one. ...
0
votes
1answer
26 views

Having an issue with a retain cycle with a uitableivew getting data from getPendingNotificationRequests

I am missing something here. I am trying to build a tableviewcontroller that will display all pending user notification in my app. Here is the code: import UIKit import UserNotifications class ...
1
vote
2answers
63 views

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 ...
3
votes
1answer
58 views

Why I get retain circle in the second example?

Why do we get strong reference circle in the second example, why we did not in the first example? class Test1 { var name: String = "Ted" lazy var greeting = { return "Hello \(self.name)" }()...
2
votes
1answer
61 views

Assets singletons and reference cycles

I currently have an Assets singleton class that provides me access to textures, sounds, and music. As my partner and I are going through the memory management stage of our project we have realized ...
2
votes
0answers
76 views

weak and unowned self only?

I have encountered some memory leaks that appear to be related to closures capturing variables not necessarily what appears to be the well-known "self retain cycles" in my code. My question in ...
0
votes
1answer
26 views

Will this code produce retain cycle ? (Core Data perform)

extension NSManagedObject{ /// Perform on main context asynchrounously /// SaveContext will be called after the block() is executed /// - Parameter block: The blcok of code passing ...
0
votes
1answer
54 views

Dismiss rootViewController on UIApplicationWillTerminate

When trying to dismiss root view controller; This code works in other functions but not in a function which is get called by UIApplicationWillTerminate notification. let appDel = UIApplication....
0
votes
0answers
37 views

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 ...
-1
votes
2answers
161 views

Why Does My App Crash due to Memory Issue?

I am dealing with an app created by some other developer. It's a complete app and has a lot of viewControllers , variables and outlets. I keep getting the a crash after I load too many images from a ...
0
votes
1answer
39 views

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 ...
3
votes
2answers
380 views

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 (...
1
vote
0answers
37 views

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?
4
votes
1answer
58 views

Memory management: retain cycle with weak var, non retain cycle with unowned. Why?

TL;DR I have a struct and a class. The struct has a reference to an instance of the class, and the object has a closure that captures the struct. If the reference to the object is unowned it seems ...
1
vote
1answer
583 views

Is this the correct place to use weak self in closure?

I have a viewController that has a collectionView outlet. In the cellForRowAt method I call this code: guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: ...
0
votes
1answer
131 views

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 ...
1
vote
1answer
328 views

Swift closure stored as a variable produces memory leak

I have a retain cycle when defining a closure as a variable. The variable is defined as below: public class MyCell: UICollectionViewCell { public var callback: ((MyCell)->Void)? } If I use ...
-2
votes
2answers
27 views

In objective-c, by using self in a view controller, does this create a strong reference cycle?

I've noticed while working with Objective-C, the compiler throws error symbols enforcing the use of either self or an underscore when using a property which I think doesn't happen as harshly when ...
0
votes
1answer
356 views

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 ...
2
votes
2answers
134 views

Avoid retain cycles in closures using weak self

We were having this debate at work about what's the best way to avoid retain cycle in a closure. We were debating following two approaches. func getStock() { [weak self] (stock)in self?....
1
vote
1answer
39 views

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 ...
2
votes
1answer
277 views

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 } } } ...
1
vote
1answer
306 views

MVVM - using closures to bind a ViewModel with a DataSource: capture list needed?

I have a viewController keeping a (strong) reference to its viewModel lazy private var viewModel: ListViewModel = { return ListViewModel() }() override func viewDidLoad() { super.viewDidLoad() ...
0
votes
0answers
56 views

Capture list in closure: why “weak”?

I read this article about implementing MVVM. It showcases a master-detail app, where there is a first viewController handling a table view and which also holds a viewModel. Such viewModel calls to a ...
2
votes
3answers
64 views

This Swift code should produce a memory leak but it doesn't. Can someone point out why?

I think I know what't a retain cycle is on Swift and why it produces a memory leak. But I wrote a small example to demonstrate it and seems the code is correctly deallocated anyway. In this example I ...
1
vote
1answer
138 views

Accessing Singletons in a Closure = Memory Leak?

Does accessing a singleton within a closure cause a retain cycle? Specifically something like this example: class TheSingleton { static let shared = TheSingleton() //THE SINGLETON enum ...
0
votes
1answer
52 views

ViewController not released from Memory

I have this view hierarchy RouterDashboardViewController : RootViewController RootViewController : UIViewController Currently, RouterDashboardViewController instance is in the navigation ...
0
votes
1answer
53 views

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 ...
6
votes
1answer
2k views

Swift closures causing strong retain cycle with self

I just want to know if I am understanding this correctly or not. So according to the apple docs when you create a closure as a property of a class instance and that closure references self(The class ...
0
votes
1answer
283 views

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....
-1
votes
1answer
82 views

Objective-C retain cycle between 2 objects

Here is the code: TestA *ta = [[TestA alloc] init]; TestB *tb = [[TestB alloc] init]; ta.b = tb; tb.a = ta; I tried to set ta = nil or tb = nil. It didn't work but ta.b = nil worked. Why?
3
votes
1answer
532 views

Swift iOS -Should Deinit get called inside child View Controller when added as a child to another View Controller?

I have a childVC(vc3) inside a parentVC(vc2) inside another parentVC(vc1). I'm doing it this way for animation purposes. What happens is I add vc3 as a child to vc2. I have a collectionView that ...
0
votes
1answer
22 views

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, ...
1
vote
1answer
171 views

Are memory retain cycles impossible in a single-ViewController-app? (Swift / IOS)

I remember from watching CS193P from Stanford University on YouTube (yes, I'm a smart bunz)... that there's this thing called a memory leak or "retain cycle" -- something really bad -- that can happen ...
-1
votes
1answer
87 views

How to avoid memory leaks with nested objects

imagine a Swift object A that has a reference to objects B and C, and that object B also has a reference to C as depicted in below: Object A: - Object B - Object C Object B: - Object C Assuming ...
2
votes
1answer
70 views

Trying to understand the reference cycle

I was reading Apple's documentation about Objective-C's reference cycles and then I tried to create one, but I can't quite understand its behaviour. Here what I have: there are two classes XYZPerson ...
0
votes
2answers
306 views

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 ...
1
vote
1answer
1k views

Why is [weak self] or [unowned self] not needed in Operation Queue?

After learning about Swift's capture list and how it can be used to avoid retain cycle, I can't help noticing something puzzling about OperationQueue: it doesn't need either [weak self] or [unowned ...
0
votes
3answers
329 views

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 ...
4
votes
2answers
588 views

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....
0
votes
1answer
448 views

Is cleaning up strong references in deinit a correct pattern?

There are several resources (blog, SO question, plus I've seen it used everywhere) that recommend removing an observer from the NotificationCenter in the deinit of the UIViewController, e.g.: deinit {...