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
1
vote
1 answer

obj-c weak self in a block: why the 2nd one doesn't need a weak self inside in two similar cases

I finally found my memory bug is caused by referring self strongly in a block. But I don't know why in a similar case, the weak is not needed: I have a CameraCaptureManager class doing image capture tasks, and a CameraViewController has a strong…
X.Y.
  • 13,726
  • 10
  • 50
  • 63
1
vote
1 answer

[Not A Type retain]: message sent to deallocated instance,objective c

I am customizing my view with gradient and adding an uilabel on it by doing the following : @implementation ECertificateViewController - (void)viewDidLoad { [super viewDidLoad]; CustomViewBackGround *bgView = [[CustomViewBackGround…
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
1
vote
1 answer

Retain cycle with ARC, NSOperationQueue and dispatch_source_t (timer)

I have a method that adds an operation to the serial NSOperationQueue. Since I want to call the method on a regular basis, I use dispatch source timer. However, this method can also be called in response to user actions. When this happens (e.g. a…
0
votes
1 answer

SwiftUI List memory leak

Here is a very simple code example that reproduces a memory leak when deleting an item in SwiftUI List. This is reproducible on iOS 15 and iOS 16 (at least). Item's are never deallocated even if they are deleted from the list. import SwiftUI class…
josip04
  • 224
  • 3
  • 13
0
votes
0 answers

How to avoid retain cycle when assigning NSDocument as NSViewController's representedObject

I am building a macOS Document based application. I am following Apple's example code, which can be found here. In Apple's example, the NSDocument assigns itself as the NSViewController's representedObject. The NSDocument also retains a strong…
0
votes
2 answers

Memory leak situation when storing a URLSession task in a property in Swift

I'm trying to understand the memory leak situation in Swift language but there is a situation that I'm still wondering. I've created a new UIViewController and call fetch function with storing the fetch task in a property without starting the task…
0
votes
1 answer

Why does this NOT leak memory? RxFeedback

class ViewModel { ... func state(with bindings: @escaping (Driver) -> Signal) -> Driver { Driver.system( initialState: .initial, reduce: State.reduce(state:event:), feedback: …
hydro1337x
  • 95
  • 5
0
votes
1 answer

Do you need unowned or weak inside a UIAlertController handler which captures self?

Does a UIAlertAction's handler need a [weak self] or [unowned self] to avoid a retain cycle, or is it fine to keep it as a strong reference? e.g. extension UIViewController { func showAlert() { let alert = UIAlertController( …
shim
  • 9,289
  • 12
  • 69
  • 108
0
votes
0 answers

How to pass a closure dismissing custom alert avoiding retain cycles

I want to create a custom alert via code. This code below is working, but I Need help on some questions about passing data and retain cycles is it right my usage of [weak self] ? I'd like to avoid Delegate Pattern, so my plan is to pass an action…
biggreentree
  • 1,633
  • 3
  • 20
  • 35
0
votes
0 answers

How to resolve "AttributeGraph: cycle" warnings in the following code?

For context, the goal of the code below is to intercept a particular type of link inside a webview and handle navigation across a tabview natively (to a separate webview displaying the desired page) rather let the webview navigate itself. However,…
Chris
  • 909
  • 8
  • 18
0
votes
1 answer

How to use Xcode static analyzer command line to generate an HTML report of warnings?

I installed llvm and use scan-build to statically analyze a very simple demo project. In this project, I intentionally created a retain cycle and Xcode can immediately display a retain cycle warning in the editor. However, if I switch to use the…
P. Tsin
  • 435
  • 4
  • 14
0
votes
1 answer

How to properly allocate/initialize a weak variable? (Swift)

I'm trying to get rid of a memory leak associated with an MKMapView. I think the main problem is that I created my entire project without using storyboard as a series of views which I manage by either setting the alpha to 0 or shrinking the view to…
jmsapps
  • 388
  • 2
  • 17
0
votes
1 answer

closure and retain cycle

I am trying to pass a closure from one class to another. But I am getting a retain cycle. I am new to closures and retain cycles. I spent a couple of hours reading tutorials but I didn't understand how to do it. I have two classes A and B. B hold an…
Ditza
  • 335
  • 2
  • 14
0
votes
1 answer

Is Java WeakReference the same as the swift weak keyword?

I know there is a similar question has been answered in this post. However, I am still confused about whether java has the retain cycle problem as java also has the WeakReference class. So, are they serving the same purpose? What the difference…
user762750
  • 159
  • 7
0
votes
0 answers

ADMob memory increase

I am looking to improve my memory usage on my app and noticed that using AdMob is adversely affecting my application. Specifically, with this code: override func viewDidLoad() { super.viewDidLoad() …
NullHypothesis
  • 4,286
  • 6
  • 37
  • 79