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
5
votes
1 answer

Avoiding retain cycle when using function as a block in swift

following is a code sample you can run in a playground import Foundation class TempNotifier { var onChange: (Int) -> Void = {t in } var currentTemp = 72 init() { // 1. onChange = { [unowned self] temp in …
Sash Zats
  • 5,376
  • 2
  • 28
  • 42
5
votes
2 answers

Does calling a method inside a block that calls another method referring to self cause a retain cycle?

Can doFirst cause a retain cycle here? @interface Example : NSObject @property (nonatomic, strong) void (^block)(); @end @implementation Example - (void)doFirst { __weak id weakSelf = self; self.block = ^ { [weakSelf…
JRG-Developer
  • 12,454
  • 8
  • 55
  • 81
5
votes
1 answer

substituting for __weak when not using ARC

I have this line of code: __weak NSBlockOperation *weakOperation = operation; which is triggering this compiler error: __weak attribute cannot be specified on automatic variable. Reason for this is I don't have ARC enabled (not ready to make the…
Ser Pounce
  • 14,196
  • 18
  • 84
  • 169
5
votes
1 answer

blocks, self, retain cycles

I'm having a bit of trouble getting my head around referencing self within a block, and not creating a retain cycle. Can you let me know if my understanding is correct: If I ever reference self within a block, it will create a retain cycle, and…
Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43
4
votes
2 answers

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. Following is the code I am using. class…
Sujal
  • 1,447
  • 19
  • 34
4
votes
1 answer

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 that both of them get deinitialised. If the…
bursyllac
  • 413
  • 1
  • 5
  • 11
4
votes
2 answers

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 pushes on vc1. Once vc1 is on scene vc2 is added to…
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
4
votes
2 answers

Confusions about weak delegate in swift

Let us suppose we have a protocol protocol MyProtocol { fun someFunc() } class AClass { var delegate: MyProtocol? } AClass doesn't care if the delegate is a class or struct. What I want is sometimes the delegate can be a class and…
echo
  • 1,244
  • 1
  • 16
  • 40
4
votes
1 answer

Is there no retain cycle in my myClass2?

Can somebody help to explain why obj2 will get deinit ? ( I think that there's a retain cycle) obj2 and obj1 are so alike: they both have a property named printNameLength, which both is a closure, which both capture self(or is it?). But obj2…
4
votes
1 answer

Avoid weak references by using function parameter types?

I have a function that accepts a block as a parameter. I can use trailing closures to pass in a block of code, but that closure must use a weak reference when retaining self. For example, see the initializer of MyClass2: class MyClass { func…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
4
votes
1 answer

How to fix retain cycle between CFXURLCache and _NSURLSessionLocal?

Was just wondering if anyone's run into this as well, and if it's anything to worry about:
Ben Guild
  • 4,881
  • 7
  • 34
  • 60
4
votes
2 answers

Retain cycle happens when passing method instead of closure

In Swift we can nice feature we didn't have in ObjC: it's possible to use a method everywhere you would use a closure. But it can lead to retain cycles. Look at this example: import Foundation class C1 { let closure: Void -> Void …
Alexander Doloz
  • 4,078
  • 1
  • 20
  • 36
4
votes
1 answer

Do we need to use weak self in blocks in Objective-C?

I noticed Apple's documentation saying we need to avoid strong reference cycles when capturing self. The block in the example is a property of self. But what if I put a block as a local variable in a dispatch_async statement? In this case, it will…
4
votes
1 answer

Swift assign function to var cause retain cycle?

I met a similar question in Swift Memory Management: Storing func in var but that didn't solve my problem. Here is my class definition: class Test { var block: (() -> Int)? func returnInt() -> Int { return 1 } deinit { …
sevenkplus
  • 178
  • 1
  • 12
4
votes
1 answer

Swift: Retain cycle with NSOperation

In my app I use an image loader class to load images from the web for a collection view. The class keeps track of the download operations and cancels them when the cells for the images are no longer visible in the collection view. This…
Leontien
  • 612
  • 5
  • 22