Questions tagged [weak-references]

A weak reference is a one that makes no claim of ownership. A weak reference keeps a reference to the object in question while it is in memory, but does not prevent the memory management system from reclaiming the associated memory when the referenced object is otherwise no longer needed. Many languages feature or support various levels of weak references, such as Swift, Objective-C, Java, C#, Python, Perl and Lisp.

A weak reference is one that does not cause the memory management system to hold on to the referenced object. It is used in cases where one wants a reference to the object in question as long as it is still needed/used elsewhere, but to also not prevent it from being reclaimed by the memory management system in the course of its normal lifecycle.

Weak references are especially important where one might have two or more objects referencing each other and you want to avoid a “retain cycle” or ”strong reference cycle”, where circular combinations of strong references will prevent the memory from ever being reclaimed. One common use of weak references in conjunction with closures and callbacks.

Some garbage-collected languages feature or support various levels of weak references, such as , , , and .

Reference counting languages, such as and , use weak (and unowned) references to prevent the establishment of additional strong references. An object that has no further strong references will be deallocated.

Certain environments have further variations on weak references such as "soft", "unowned", "phantom", which have very specific meanings in those environments.

1131 questions
14
votes
2 answers

How do weak and strong references look like in objective-c?

Wikipedia states "In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector". How do those two types of references look like in code? Does a weak reference is a…
Centurion
  • 14,106
  • 31
  • 105
  • 197
14
votes
4 answers

Swift weak lazy variable won't compile

To demonstrate this problem, I made a vanilla Cocoa project. Here is the AppDelegate.swift: import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { weak lazy var isGood : NSNumber? = { return true }() …
LShi
  • 1,500
  • 16
  • 29
14
votes
4 answers

Can't make weak reference to closure in Swift

Update: I tried writing it without making it weak, and there doesn't seem to be a leak. So maybe the question is no longer necessary. In Objective-C ARC, when you want to have a closure be able to use itself inside of the closure, the block cannot…
user102008
  • 30,736
  • 10
  • 83
  • 104
14
votes
2 answers

using python WeakSet to enable a callback functionality

I'm investigating if I can implement an easy callback functionality in python. I thought I might be able to use weakref.WeakSet for this, but there is clearly something I'm missing or have misunderstood. As you can see in the code I first tried with…
thomas
  • 432
  • 5
  • 12
14
votes
5 answers

Suitable collection class for event listeners in Java

Related: Does java have a "LinkedConcurrentHashMap" data structure? I am looking for a collection class to hold references to event listeners. Ideally I would like the collection to have the following properties (in order of priority): Maintains…
finnw
  • 47,861
  • 24
  • 143
  • 221
14
votes
1 answer

Weak property is set to nil in dealloc but property's ivar is not nil

I noticed the following in Objective-C with ARC enabled: Let's have simple class A and autosynthesized weak property @interface A @property (nonatomic, weak) id refObject; @end @implementation A @end And second class B with dealloc…
13
votes
3 answers

How can I maintain a weak reference on a COM object in C++?

In my application, I'm hooking various functions for creating COM objects (such as CoCreateInstanceEx) to get notified whenever some object is created. I'm keeping track of all created objects in a std::list and I'm iterating over that list to do…
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
13
votes
3 answers

Does Dart/Flutter have the concept of weak references?

I'm in the early stages of learning Dart & Flutter. I'm looking at how to implement an eventbus, which works fine, but I've noticed that Widgets (and/or their associated state) hold a strong reference to the (global) eventbus, causing a memory leak.…
hunter
  • 872
  • 10
  • 26
13
votes
1 answer

why use WeakReference on android Listeners?

I am working on a large code base, and see in many places this type of code: public static class RequestCustomData implements View.OnClickListener { WeakReference mainActivity; public RequestCustomData(MainActivity activity)…
gmmo
  • 2,577
  • 3
  • 30
  • 56
13
votes
3 answers

Two weak variables referencing each other in Swift?

I'm making another attempt today to try to understand retain cycles and weak references in Swift. Reading through the documentation, I saw the following code example where one of the referencing variables is marked as weak to prevent a retain…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
13
votes
3 answers

Swift Closures - Capturing self as weak

I am trying to resolve a closure based strong reference cycle in Swift. In the code below, object is retained by the owning view controller. ProgressHUD is a UIView that's also retained by the owning view controller. ProgressHUD is leaked every time…
Fergal Rooney
  • 1,330
  • 2
  • 18
  • 31
13
votes
1 answer

NodeJS in-memory cache with memory pressure awareness

I'm coming from Java world, and there are plenty implementations of (local) in-memory caches. Moreover in Java world there are SoftReference and WeakReference, and they're, by definition, ideal for cache implementation(s). I know that JavaScript…
Tomo
  • 6,847
  • 1
  • 22
  • 32
13
votes
1 answer

Can I have a weak static pointer?

Can I have a static pointer that is weak in objective-c? I know it compiles, but I want to know if it will behave as I expect a weak pointer to behave. __weak static HMFSomeClass *weakStaticPointer;
Hackmodford
  • 3,901
  • 4
  • 35
  • 78
12
votes
4 answers

Which of these objects are eligible for garbage collection?

This is a question I was asked at my interview recently: Which 'Random' object(s) would get collected during the 'GC.Collect()' call? String a = new Random().Next(0, 1) ==1 ? "Whatever 1" : "Whatever 2"; String b = new WeakReference(new…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
12
votes
1 answer

Should adapters in Android be static inner classes or non-static inner classes

I have a ListView in an Activity and I am setting a custom adapter to the ListView. Should my adapter class be: private static class MyAdapter extends ArrayAdapter or private class MyAdapter extends ArrayAdapter I guess it should make no…
dnkoutso
  • 6,041
  • 4
  • 37
  • 58