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
19
votes
9 answers

Why doesn't .NET have a SoftReference as well as a WeakReference, like Java?

I really love WeakReference's. But I wish there was a way to tell the CLR how much (say, on a scale of 1 to 5) how weak you consider the reference to be. That would be brilliant. Java has SoftReference, WeakReference and I believe also a third type…
Nathan Evans
19
votes
2 answers

How can I use a std::map with std::weak_ptr as key?

How can I use std::weak_ptr as key for a std::map as shown in the following code? #include #include int main() { std::map< std::weak_ptr, bool > myMap; std::shared_ptr sharedptr(new int(5)); std::weak_ptr
user987280
  • 1,578
  • 1
  • 14
  • 24
19
votes
8 answers

Why do we need weak reference in java

I understand that weak references are at the mercy of the garbage collector, and we cannot guarantee that the weak reference will exist. I could not see a need to have weak reference, but sure there should be a reason. Why do we need weak reference…
18bytes
  • 5,951
  • 7
  • 42
  • 69
18
votes
1 answer

Any practical example of long weak reference?

Is anybody has a pratical example of "long" weak reference (not short) ? Is this only for internal usage ?
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
17
votes
2 answers

Pickling weakref in Python

I am still pretty new to Python and even newer to pickling. I have a class Vertex(ScatterLayout) with a __getnewargs__(): def __getnewargs__(self): return (self.pos, self.size, self.idea.text) My understanding is that this will cause the pickle…
David Poxon
  • 2,435
  • 4
  • 23
  • 44
17
votes
7 answers

Compacting a WeakReference Dictionary

I've got a class Foo with a property Id. My goal is that there are no two instances of Foo with the same Id at the same time. So I created a factory method CreateFoo which uses a cache in order to return the same instance for the same Id. static Foo…
dtb
  • 213,145
  • 36
  • 401
  • 431
16
votes
1 answer

Creating a regular weak-reference in Javascript using WeakMaps

I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener. So I was very excited to find WeakMaps, until I…
16
votes
3 answers

weak reference to object containing a nested strong reference, and garbage collection

Suppose I have a weak reference to a car which has an ordinary (strong) reference to an engine. No other references to the car or the engine exist. Can the engine be garbage collected?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
16
votes
4 answers

Thread Safety of WeakReference

When using a WeakReference, how can we be sure than the target is not collected between the .IsAlive and .Target calls? For example: if (myWeakReference.IsAlive) { // How can we be sure the object is still alive while here? …
Khash
  • 2,500
  • 4
  • 30
  • 56
16
votes
4 answers

Python: dereferencing weakproxy

Is there any way to get the original object from a weakproxy pointed to it? eg is there the inverse to weakref.proxy()? A simplified example(python2.7): import weakref class C(object): def __init__(self, other): self.other =…
robyschek
  • 1,995
  • 14
  • 19
15
votes
2 answers

Swift warning: 'weak' should not be applied to a property declaration in a protocol

Looks like weak references will be disallowed in protocols. So what am I supposed to do if I wanna add a weak reference? Any better idea? protocol PipelineElementDelegate: class { func someFunc() } protocol PipelineElement { weak var…
boog
  • 1,813
  • 3
  • 18
  • 21
15
votes
2 answers

Calling method using optional chaining on weak variable causes EXC_BAD_ACCESS

Update: This is fixed in Xcode 6 beta 6. The following code causes an EXC_BAD_ACCESS on the delegate?.thing() line: @class_protocol protocol Fooable { func foo() } class Bar : Fooable { func foo() { } } weak var delegate: Fooable? let bar =…
user102008
  • 30,736
  • 10
  • 83
  • 104
15
votes
6 answers

Garbage Collection should have removed object but WeakReference.IsAlive still returning true

I have a test that I expected to pass but the behavior of the Garbage Collector is not as I presumed: [Test] public void WeakReferenceTest2() { var obj = new object(); var wRef = new WeakReference(obj); wRef.IsAlive.Should().BeTrue();…
TechnoTone
  • 181
  • 2
  • 6
15
votes
9 answers

Why are weak pointers useful?

I've been reading up on garbage collection looking for features to include in my programming language and I came across "weak pointers". From here: Weak pointers are like pointers, except that references from weak pointers do not prevent…
Imagist
  • 18,086
  • 12
  • 58
  • 77
15
votes
2 answers

strange WeakReference behavior on Mono

Testing code that uses WeakReference failed for me using Mono 2.11.3 (SGen) as well as the stable 2.10.8 version. In a simple code like this object obj = new object(); WeakReference wr = new WeakReference(obj); Assert.IsTrue(wr.IsAlive); obj =…
actionresult
  • 399
  • 2
  • 8