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.

301 questions
3
votes
1 answer

calling super from within a GCD dispatch_async block: is it safe?

I'm in a bit of a pickle. I know that calling [self methodName] from within a block will lead to a retain cycle. However in this class due to multithreading I cannot allow execution of the method that the block is accessing from anywhere else other…
Ælex
  • 14,432
  • 20
  • 88
  • 129
2
votes
0 answers

SwiftUI retain cycle with class

I have a View which contains some kind of a controller. I need to have ability to trigger View's method from the controller. But I am getting retain cycle that I can't solve. Such View never destroy from memory. Any way to break that retain cycle? I…
bodich
  • 1,708
  • 12
  • 31
2
votes
1 answer

Weird weak self and retain cycle behaviour

Let's consider following code: // Just for easier testing protocol Printer { var delayer: Delayer { get } } // Retain cycle class Printer1: Printer { private func action() { print("action") } private(set) lazy var…
MrKew
  • 333
  • 1
  • 14
2
votes
1 answer

Why doesn't Swift ARC break property references first to prevent retain cycles?

I was just interested to know how ARC works when a block of code goes out of scope. I assume all the reference variables are set to nil/destroyed with properties excluded and all objects are destroyed that have a 0 reference count. This scenario: A…
Frascale
  • 23
  • 4
2
votes
1 answer

Why Is There No Retain Cycle In ReceiveValue Block Combine Subscription

I'm determined to fully understand why this isn't causing a reference cycle. And in general what is happening at each stage of memory management here. I have the following setup: struct PresenterView: View { @State private var isPresented =…
jeh
  • 2,373
  • 4
  • 23
  • 38
2
votes
1 answer

xcode memory graph not show retain cycle

My swift code is below. I have used self in closure but when I run project, memory graph does not show retain cycle. I am wrong about there is a retain cycle? class ViewController: UIViewController { private var counter = 0 private var…
ahmetvefa53
  • 581
  • 6
  • 20
2
votes
1 answer

Swift 5 - Simulator launching with white screen - Instruments

I was trying to check the memory leak for my project using Instruments. Although, I could launch instruments but while its running the simulator is launching with white screen only. So that I could not debug my application to find the retain cycle.…
2
votes
0 answers

NSWindow Leak in Minimal Example (ARC turned on)

I'm trying to figure out why an NSWindow instance is never getting freed in a project I'm working on. I've managed to get the minimal repro case down to the following single file project that you can execute from the command line. As far as I can…
Mason
  • 33
  • 1
  • 5
2
votes
2 answers

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 subviews: [UIView] { get } open var window:…
ahmetvefa53
  • 581
  • 6
  • 20
2
votes
1 answer

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 that we may have a serious leak being created, and…
2
votes
1 answer

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: indexPath) as? MediaPhotoCell else {fatalError("Could…
2
votes
2 answers

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?.dismissActivityIndicator() } vs func…
Vandan Patel
  • 1,012
  • 1
  • 12
  • 23
2
votes
3 answers

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 have two objects that retain each other (creating…
IgnazioC
  • 4,554
  • 4
  • 33
  • 46
2
votes
1 answer

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 and XYZPersonSpouse. They have properties for…
Tigran Iskandaryan
  • 1,371
  • 2
  • 14
  • 41
2
votes
1 answer

How to deinit CollectionView class

In my project has 2 ViewControllers (ViewController and DetailViewController). On the first there are table view in UIScrollview. On the second - buttons in UICollectionView with images and links. When i popup in DetailViewController many times, the…
Eugeny
  • 308
  • 4
  • 12