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
10
votes
2 answers

Acquiring a regular reference from a weakref proxy in python

Is there a way to get a proper reference for an object for which I get a weakref proxy? I've gone through the weakref module's documentation and coulnd't get an answer there, or through poking a weakproxy object manually.
itai
  • 1,566
  • 1
  • 12
  • 25
10
votes
2 answers

How to force full garbage collection in .NET 4.x?

I've a problem with WeakReferences in .NET 4.x, I was running tests to make sure some objects were not referenced anymore (using WeakReferences) and I noticed the behavior is not consistent across framework versions: using System; using…
Guillaume86
  • 14,341
  • 4
  • 53
  • 53
10
votes
2 answers

Objective-C: Weak attritube don't work as expected

Possible Duplicate: Why do weak NSString properties not get released in iOS? I'm a newbie to Objective C and I've got some questions that I cannot answer myself. I have a block of code for testing __weak variable (I'm using ARC, of course):…
hoang Cap
  • 704
  • 6
  • 18
9
votes
1 answer

WeakHashMap and strongly referenced value

Javadocs says "When a key has been discarded its entry is effectively removed from the map". But unless there is another thread that occasionally removes such Map.Entry entries, won't the value objects be strongly referenced by the map? But since…
Ustaman Sangat
  • 1,505
  • 1
  • 14
  • 26
9
votes
2 answers

Will a WeakHashMap's entry be collected if the value contains the only strong reference to the key?

I need to associate some data with a key for its lifetime, so I am using a WeakHashMap. However, in addition I need to get a key by its corresponding value. The easy way to do it is to hold on to the reference when creating a value: public class Key…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
9
votes
1 answer

Create Weak Multimap with Google Collections

Is there an equivalent to the nice MapMaker for MultiMaps? currently i create the cache like this: public static Map> personCache = new MapMaker().weakKeys().makeMap(); the whole point of MultiMap is to avoid the nested…
Andreas Petersson
  • 16,248
  • 11
  • 59
  • 91
9
votes
2 answers

How to remove a weakReference from a list?

I've got a list of weakReferences to objects in java. How do i write a method that gets the real object instance and removes it's weak reference from this list? thanks.
Adibe7
  • 3,469
  • 7
  • 30
  • 36
9
votes
2 answers

Initialise WeakReference object to avoid null check

Given the below example code is there a way to initialise total such that I do not have to do a null check later when I use it. I am not able to pass the value into the constructor. public class SampleCode { private WeakReference
running-codebase
  • 998
  • 2
  • 12
  • 17
9
votes
1 answer

Knowing where retain cycles are and removing them

I was wondering if there was an easy way (or at least a way) to find out where retain cycles exist in your program. Also, if I then know where these retain cycles exist, depending on their types (e.g. variable or closure), how do I make them weak. I…
J.Treutlein
  • 963
  • 8
  • 23
9
votes
2 answers

One Liner: WeakReference-to-a-Lambda Event Handler

Can you see downsides to this one-liner other than the fact that multiple uses of it would violate the DRY principle? It seems straightforward but the fact that I haven't seen others propose it makes me wonder if there's a downside to it. This bit…
Ben Gribaudo
  • 5,057
  • 1
  • 40
  • 75
9
votes
7 answers

How to detect when an object is no longer referenced

Is there a way to create register a handler that will be called exactly at the time when the last reference to a certain object is released? An example would be an object that is backed by a physical data file and once the object become…
VoidPointer
  • 17,651
  • 15
  • 54
  • 58
9
votes
3 answers

WeakReference is dead

I am toying with an event aggregator using a weak reference to the method in my subscriber object I wish to handle the event. When subscribing the weak reference is created successfully and my subscribers collection is updating accordingly. When I…
user1548266
9
votes
5 answers

How can I reference __weak self in dealloc method

I have a method called in various places called "cancelAllPendingDownloads" This is a general method that cancels various jobs and updates internal counters. Problem happens when it is called within the dealloc method -(void)dealloc { [self…
Avba
  • 14,822
  • 20
  • 92
  • 192
9
votes
5 answers

SQLiteOpenHelper synchronization

So I've come up with some idea and I'm wondering if it is realizable. Let's say I've multiple tables(database models) each of them is represented by some class.I don't wont to use singleton pattern with the open helper so I've created some simple…
Teodor
  • 300
  • 1
  • 13
9
votes
3 answers

Weak hashmap with weak references to the values?

I am building an android app where each entity has a bitmap that represents its sprite. However, each entity can be be duplicated (there might be 3 copies of entity asdf for example). One approach is to load all the sprites upfront, and then put…
Razor Storm
  • 12,167
  • 20
  • 88
  • 148