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
0
votes
1 answer

Unsafe raw pointer from weak_ptr - safety guaranteed by context

I am storing a weak_ptr for avoiding circular structure, and I know that in some particular context the shared_ptr is still valid. Can I get the raw pointer from weak_ptr without casting to shared_ptr (which involves memory write)? (note: this is…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
0
votes
1 answer

Nil'ing weak pointers in Objective-C ARC? Slow like hell?

In the following question, it was asked how the niling of weak pointers works in Objective-C: How does the ARC's zeroing weak pointer behavior implemented? The answer pointed to this document that seems to include the answer:…
gexicide
  • 38,535
  • 21
  • 92
  • 152
0
votes
2 answers

Why aren't weak references built-in types in the CLI

I'm wondering why weak references are not built-in types which are treated similar to standard object references by the garbage collection. In C#, you must either use the WeakReference class (which has a significant performance and memory impact as…
LionAM
  • 1,271
  • 10
  • 28
0
votes
1 answer

It seems that an iOS app can use ARC and support iOS 4.3 or 4.x, but not if it is using a "weak" reference?

I have used ARC and built apps before that supported iOS 4.3, but as soon as I started using weak, because a Tree has a strong reference to node, and node has a reference back to the tree, which should be a weak reference: @property (weak,…
Jeremy L
  • 3,770
  • 6
  • 41
  • 62
0
votes
2 answers

Setting strong references objects to nil with ARC enabled?

I am developing an iPhone app with ARC option enabled. i am creating IBOutlets in .h files which are connected from file owners icon to .xib elements.for eg. IBOutlet UIButton *bt; @property(nonatomic,retain)IBOutlet UIButton *bt; in .m file, i am…
0
votes
1 answer

The reason that IBOutlets are weak in Xcode

I read the related posts and don't quite get it. Should IBOutlets be strong or weak under ARC? My understanding is that since the top level view already has a strong pointer to the outlet automatically, so that we should not make another strong…
Philip007
  • 3,190
  • 7
  • 46
  • 71
0
votes
1 answer

Android WeakReference not behaving as expected

As I know, from the spec (and from the standard non-android) java, If an object is only "Weak Referenced", that is, only reachable by weak references - it will be collected by the garbage collection the next time it runs. In my android app, for some…
Yoav Schwartz
  • 678
  • 1
  • 8
  • 18
0
votes
1 answer

tableView:cellForRowAtIndexPath: not called because array is empty?

My tableView wasn't loading because tableView:cellForRowAtIndexPath: wasn't being called. The NSLog messages that I have written inside tableView:cellForRowAtIndexPath: did not appear in the console. I had a weak NSArray @property called…
0
votes
1 answer

Can you use System.Threading.Thread.CurrentThread as a key in a dictionary

I'm currently trying to implement a minimalistic ThreadLocal class. I know this is implemented in DotNet 4, but I'm in a situation where its not possible to upgrade. My idea is to use a very simple class that holds a weak hashtable and just stores…
Bjorn
  • 972
  • 1
  • 8
  • 21
0
votes
1 answer

Trying to figure out when when weak reference starts to be removed

I am reading about Weak References. I am using the code to study from here. It is very simple. private void doFunction() throws InterruptedException { Map map = new HashMap(); myMap = new…
Jim
  • 18,826
  • 34
  • 135
  • 254
0
votes
1 answer

Dynamically generated weak event failing for Action calls

I am using the SharpObservation framework to dynamically generate weak event references. When I reference an action from within the delegate I get a TypeAccessException. Attempt by method 'DynamicClass.Construct(System.EventHandler1,…
Telavian
  • 3,752
  • 6
  • 36
  • 60
0
votes
3 answers

Why is Visual Studio 2008 stuck in debug mode when compiling

I have a .NET project that for some reason gets stuck in debug mode. I've changed the compile mode from debug to release in the toolbar, but my project ends up in the debug directory anyway. Seems like VS is not updating the SLN file or something.…
Mark
0
votes
2 answers

How to pass null into WeakReference constructor?

http://msdn.microsoft.com/en-us/library/xt0a1s34(v=vs.100).aspx Parameters target Type: System.Object The object to track or null. As far as I can understand this, it means that 'null' is a valid parameter for the WeakReference object... The…
Budda
  • 18,015
  • 33
  • 124
  • 206
-1
votes
1 answer

Rust learning question tree example. Update element inside Rc

I'm studying rust and I'm stuck on a question about Rc, Weak and RefCell. The use case is to implement a fully functioning tree, where each node has one parent and a list of children. The documentation provides a good starting point: use…
hasdrubal
  • 1,024
  • 14
  • 30
-1
votes
1 answer

Weakref to an instance does not work after deletion, but works for the whole class when deleted

Say we have a class Test which has a static method as so: import weakref class Test: @staticmethod def hello(): return 'Hello' We create an instance of Test - call its method, we also create a weak reference to the instance as well.…
CodeCop
  • 1
  • 2
  • 15
  • 37