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

Python, inconsistent results vs docs: object deleted but weak ref still 'active'

There are many questions out there regarding this subject which I already checked. Example dated 2014! One can also find the 'canonical' documentation here. I don't know why I cannot reproduce the docs: import weakref, gc class A: pass a = A() b…
deponovo
  • 1,114
  • 7
  • 23
-1
votes
1 answer

Weak reference to delegate/coordinator is lost on iOS 12 and 11, works on iOS 13/14. Swift 5

I am having a weird issue with weak reference. I am using a coordinator pattern and child VCs are communicating with coordinator through delegates. Whenever I am pushing a new VC to navigation stack, I set vc's delegate to conforming coordinator. On…
-1
votes
1 answer

Why the garbage collector does not garbage my instances?

I am writing some tests to have a better understanding of how works the .NET Garbage Collector in order to build a framework without memory leak. But I am facing an unexpected behavior on my first and very simple test. Here is my knowledge about the…
fharreau
  • 2,105
  • 1
  • 23
  • 46
-1
votes
1 answer

How to resolve warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" in this case?

My PhotosListCollectionViewController.h file: @interface PhotosListCollectionViewController : UICollectionViewController { FooterView *footerView; PhotosListCollectionViewViewModel…
Tkas
  • 302
  • 3
  • 14
-1
votes
2 answers

Android - Static Nested Class (context)

I've a static AsyncTask and i need to get context, what can i do to get it? I tried using WeakReference, example: private WeakReference activityReference; FetchPositionAsyncTask(ScanActivity context) { …
-1
votes
1 answer

Circular reference - break on single reference

TL;DR Is there any way to create a weak reference that will call a callback upon having 1 strong reference left instead of 0? For those who think it's an X Y problem, here's the long explanation: I have quite a challenging issue that I'm trying to…
-1
votes
1 answer

WeakEventManager fails to handle event in attached behavior

I have a WPF attached behavior that handles the MouseEnter and MouseLeave events for an UIElement. I am trying to prevent a memory leak by switching to use the WeakEventManager, but now the code no longer handles the event. When I use (see below): …
Zamboni
  • 7,897
  • 5
  • 43
  • 52
-1
votes
3 answers

Why should I use Weak/SoftReference with ReferenceQueue argument?Why cannot I poll original reference and check if null?

As I understand working use case of detecting when object was collected and memory already free for weak/soft references it polling this queue and when reference appear in queue we can be sure that memory free. WeakReference ref = new WeakReference…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
-1
votes
2 answers

self inside block called by super

In this case, weakself is needed to avoid retain cycle [self showMethodA:^{ [weakself showMethodB]; }]; Will this case cause a retain cycle? [super showMethodA:^{ [self showMethodB]; }];
Ted
  • 22,696
  • 11
  • 95
  • 109
-1
votes
1 answer

assign in NON-ARC and retain cycle

How retain cycle was tackled in NON-ARC ! I know assign is an alternative to weak. weak will have its value set to nil If object allocated is deallocated whereas assign is not so how it was done earlier can anybody help me understand
vinoth.kumar
  • 92
  • 12
-1
votes
1 answer

Objective-C weak reference zombie

I'm trying to create a zombie object to detect sending messages to a deallocated object. Say i have a strong property object A with a weak reference to object B. When B is deallocated my weak reference becomes nil but calling a method e.g [obj1.obj2…
stefos
  • 1,235
  • 1
  • 10
  • 18
-1
votes
1 answer

Destroy target of WeakReference

I'm writing a small MemoryManager for my WPF application and reached the following problem. What i do: I store a lot of instances as a WeakReference in a IList. Later, when i want to free all memory, i want to destroy all alive…
BendEg
  • 20,098
  • 17
  • 57
  • 131
-1
votes
1 answer

Understanding weak pointers in block

The following function will not print "nil1" or "nil2" if I call it once. However, if I put it in a loop, some iterations will print "nil2" ONLY. What's going on? If user is nil, why "nil1" is not printed? After remove the __weak directive before…
-1
votes
1 answer

Sanity-check for iOS API versions used within an application

I have an application where I am supporting a deployment target of 4.3 with a base SDK of 6.x. I find myself having to weak link API methods and provide alternative workarounds for older runtime based on strategies found in the document "Using…
Mark Kang
  • 161
  • 11
-1
votes
1 answer

Different among __weak, __block and __assign?

1) Wikipedia said: "The difference between weak (_weak) and assign (_assign) is that when the object the variable pointed to is being deallocated, whether the value of the variable is going to be changed or not. weak ones will be updated to nil and…
DungLe
  • 265
  • 1
  • 3
  • 10
1 2 3
75
76