Questions tagged [strong-references]

Strong references are regular references that are protected from garbage collection, in contrast to weak references.

Strong references are regular references that are protected from garbage collection, in contrast to .

86 questions
5
votes
1 answer

Are willset and didset considered closures in Swift?

I understand the purpose of willset and didset my I am not sure if they are considered closures. If they were closures, shouldn't the following code produce a strong reference cycle? var myProperty : Int = 0 { didSet { self.callMyMethod() } }
agy
  • 2,804
  • 2
  • 15
  • 22
5
votes
1 answer

Setting a Class Object as a property of another Class in Objective-C

I'm pretty new to Objective C so maybe this is really simple, but I built a class which will store among other things a class reference. This is what I have in my .h file: @property (nonatomic, assign) Class *filterClass; Where I'm getting suck is…
Ryan
  • 414
  • 1
  • 6
  • 16
4
votes
1 answer

Understanding weak and unowned reference in Swift under the hood

I want fully understand what going inside weak and unowned referance in Swift. For this i read MikeAsh and got some questions. What already known: when there is no weak (and, i suppose, unowned) object reference, the strong reference counter is…
4
votes
2 answers

What's the difference between __weak and __strong attributes in iOS?

There is the code in one opensource project: - (id) initWithContentPath: (NSString *) path parameters: (NSDictionary *) parameters { NSAssert(path.length > 0, @"empty path"); playPath = path; self = [super initWithNibName:nil…
why
  • 23,923
  • 29
  • 97
  • 142
3
votes
1 answer

Does Xcode Memory graph offer any smart visual indicators for strong references that aren't memory cycles?

As a follow up to my previous How can I create a reference cycle using dispatchQueues?: For the strong references (that create leaks, but aren't reference cycles) e.g. Timer, DispatchSourceTimer, DispatchWorkItem, the memory graph doesn't create a…
mfaani
  • 33,269
  • 19
  • 164
  • 293
3
votes
2 answers

Avoiding strong reference when passing a method to a function

When passing a method to a function that takes a closure, I can use either someFunc(closure: someMethod) orsomeFunc() { [unowned self] in self.someMethod() }`. The first one is shorter but makes a strong reference. How can I use it while avoiding…
Dam
  • 579
  • 6
  • 22
3
votes
1 answer

convert the reference back to a strong one inside the closure, memory management, swift

I'm experimenting the retain cycle in closure like following class Sample { deinit { print("Destroying Sample") } func additionOf(a: Int, b:Int) { print("Addition is \(a + b)") } …
3
votes
2 answers

Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

With a NSNotificationCenter block, I have to use [unowned self] to avoid a strong reference cycle: NSNotificationCenter.defaultCenter() .addObserverForName(UIApplicationWillEnterForegroundNotification, object: nil, queue: nil, …
TruMan1
  • 33,665
  • 59
  • 184
  • 335
3
votes
2 answers

How to change a weak reference to a strong reference?

i connect to a client with RPyC and call a Service exposed method with a parameter object. I want to take this object from the exposed method and do somethings with it, but this object is weakly-referenced and at the time i want to access its data:…
Paula
  • 95
  • 1
  • 9
3
votes
1 answer

Strong Reference class

Why is there no java.lang.ref.StrongReference class in jdk1.7? (see JDK-6392701) I am trying to implement a behavior that needs to be able to store Objects in different reference strengths. So my first thought was to use a field of type Reference
Blank Chisui
  • 1,043
  • 10
  • 25
2
votes
1 answer

Weak, strong references and garbage collection

I have two situations: When an object (has only strong refs) loses all of its strong references, it becomes available for garbage collection. When the object has only weak references, it also becomes available for garbage collection. In what…
2
votes
1 answer

Confused on closure strong reference cycle?

class HTMLElement { let name : String let text: String? //Declaring a lazy variable that has a strong reference to this closure lazy var asHTML: Void -> String = { //Optional binding here if let text = self.text { …
2
votes
1 answer

Example of __strong usage in Objective C

I was reading about __strong reference and __weak reference usage here: Explanation of strong and weak storage in iOS5 I tried writing a bit of code to demonstrate this knowledge. However, the __strong did not keep the object in memory as it was…
rtsao
  • 199
  • 1
  • 14
2
votes
0 answers

Weak properties & prepareForSegue: one property becomes null and the other one not

I'm really confused about what happens in prepareForSegue at weak/strong properties. Let me explain: I have two simple view controller: ViewController1 ViewController2 connected with one segue from ViewController1 to ViewController2. Both view…
2
votes
1 answer

Strong reference cycles with closures

I'm trying to understand when I need to be on the look out for possible memory leaks caused by strong reference cycles. From what I've been able to glean from the swift documentation, using a self reference in a closure declared as an instance…
kellanburket
  • 12,250
  • 3
  • 46
  • 73