Questions tagged [reference-cycle]

15 questions
6
votes
2 answers

how to properly implement Observer in python when the observer is [should be] destroyed

I'm implementing an observer-observable pattern in python: This is the Observable class: class Observable(object): def __init__(self, value): self.value = value self.observers = [] def set(self, value): old =…
fferri
  • 18,285
  • 5
  • 46
  • 95
4
votes
2 answers

Strong, weak or unowned in reference cycle with Timer

I have a UIViewController that has a reference to a Timer object that is calling a closure repeatedly. The Timer object captures self in its block. As far as I understand this leads to a retains cycle between the View Controller and the block. There…
surToTheW
  • 772
  • 2
  • 10
  • 34
3
votes
0 answers

Reference cycles when using inspect module

The documentation says: Note Keeping references to frame objects, as found in the first element of the frame records these functions return, can cause your program to create reference cycles. Once a reference cycle has been created, the lifespan of…
x-yuri
  • 16,722
  • 15
  • 114
  • 161
3
votes
2 answers

Why does adding a closure capture list prevent my instance from being deallocated?

class Name { var name: String init(name: String) { self.name = name } deinit { print("\(name) deinit") } } var x: Name? = Name(name: "abc") var someClosure = { print("\(x?.name)") } someClosure() x =…
李茂琦
  • 43
  • 3
3
votes
1 answer

How to remove strong reference cycle from closure from method?

Here I have some examples for closure strong reference cycles. If I assign a closure to a stored property, I can use a closure capture list to make the captured reference unowned/weak. But if I assign a method to a stored property closure or assign…
Binarian
  • 12,296
  • 8
  • 53
  • 84
1
vote
0 answers

Deleting object's reference itself inside a function of object in JavaScript

I want to remove object itself by function of it from a object collection. If I code like below does it course to reference cycle or any other error. const obj_collec={}; function Obj(id){ this.id=id; this.remove=function(){ …
Chamod
  • 587
  • 2
  • 6
  • 17
1
vote
2 answers

Is it possible to keep track of an object from its finalizer, to detect accidental resurrection of the object by a different object's finalizer?

One of the many issues with finalize methods in Java is the "object resurrection" issue (explained in this question): if an object is finalized, and it saves a copy of this somewhere globally reachable, the reference to the object "escapes" and you…
smithaiw
  • 107
  • 6
1
vote
1 answer

Using a [weak self] for a HTTP Request

I have a question regarding the need of using [weak self] in closures and HTTP requests. As example we have a HTTP request who triggers a closure on completion: func saveBla() { blaManager.saveBla(bla) { error in self.pay5euro() …
R Pelzer
  • 1,188
  • 14
  • 34
1
vote
1 answer

Does @synchronized(self) in a block lead to a retain cycle?

Let's say I want to do @synchronized(self) within a block. I suppose this will lead to a retain cycle, so normally we would re-write it like this: -(void)myMethod { __weak TheClass * weakSelf = self; …
0
votes
0 answers

publishing value of NSObject by KeyPath using AsyncStream - reference cycle problem

I'm plaing with concurrency features of Swift. I've created helper function which returns AsyncStream with values published by NSOBject implementations. Sort of code below. func asyncStreamFor (_ root: Root, keyPath:…
0
votes
1 answer

Is there an annotation that triggers a warning if an object is passed that captures enclosing ‘this’?

I have a Kotlin function with this signature: fun registerDisposer(obj: Any, disposer: Closeable) What the function does is attach disposer to a phantom reference and arrange it to be closed when obj is garbage-collected (i.e. when the phantom…
user3840170
  • 26,597
  • 4
  • 30
  • 62
0
votes
2 answers

Vala closure reference cycle

I'm writing a class in Vala where I bind two of the same object's properties together, with a closure to transform one to the other. class Foo : Object { public int num { get; set; } public int scale = 2; public int result { get; set;…
Albert Tomanek
  • 387
  • 4
  • 13
0
votes
1 answer

Callback strong Reference Cycle

Is the following creating a strong reference cycle? I have a feeling it is because I'm referencing self within callback. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker…
AnonProgrammer
  • 239
  • 1
  • 11
0
votes
0 answers

nodejs - does forEach create a reference cycle?

I am a newbie in NodeJS, my background is Swift. When facing forEach, I wonder if it would create a reference cycle? const array = [1, 3, 2, 4] arrach.forEach(function(element) { doSomething(element) }) function doSomething(number) { //…
quanguyen
  • 1,443
  • 3
  • 17
  • 29
0
votes
2 answers

Does assigning a weak pointer to a strong pointer copy the object?

The common pattern to avoid capturing self within a Block is to create a weak self outside the Block and use this to create a "locally strong" version of self within the Block (inner self). __weak ClassX *weakSelf = self; [someOtherObject…