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

What the output should be there?

The output should be strString = değiştim wkString = NULL but it is not. WHY? #import @interface learnARC : NSObject { NSString *strString, __weak *wkString; } @property (strong) NSString *strString; @property…
0
votes
1 answer

WeakReference and ReferenceQueue performance matter

I use RefQueue, to track which WeakRef's are not pointing to object anymore. But I'm wonering how it works. When I tell WeakReference to register it self in particular queue, and then after some time I do: private void removeDumpReferences() { …
abc
  • 2,371
  • 3
  • 25
  • 36
0
votes
3 answers

Map Size incorrect in Java. If I wrap Key and Value in WeakReference and then add into HashMap, printed size is different than expected

If I don't comment line 1 and comment line 2, line 1 causes OutOfMemoryError. If I do the reverse, it does not causes OutOfMemoryError because are wrapped in WeakReference. But i can't understand the output of line 3 ---- i : 3869 Size…
0
votes
1 answer

How to use Soft/WeakReference classes in a Android App?

I'm maintaining a big app with a huge number of images. My main problem is the app crashes when I use it because it produces out memory error. I'm trying to use SoftReferences and WeakReferences, I've read about it, but I don't know if I have to use…
beni
  • 3,019
  • 5
  • 35
  • 55
0
votes
1 answer

Memory-Leak Image-Gallery Android

I'm trying to implement an image gallery in android. The code based on http://www.mobisoftinfotech.com/blog/android/android-gallery-widget-example-and-tutorial/ and i've changed some details. I'm using WeakReference and it seems, that when i've too…
Frame91
  • 3,670
  • 8
  • 45
  • 89
0
votes
1 answer

Cache to map IntPtr handles to C# class instances in pinvoke callbacks

I'm writing a C# wrapper around a native dll that uses opaque pointers to identify resources. A typical example would be something like typedef struct session session; typedef struct track track; void* sessionCreate(int arg, session** sess); void …
Tom Davies
  • 2,386
  • 3
  • 27
  • 44
0
votes
1 answer

Java WeakHashMap reference not being updated

In the code below I create a Pen object and initialize it's color to white. In the constructor of Pen, after setting the field 'penColor' to the value passed into the constructor, I update a global static weak hashmap that I'm keeping where the KEY…
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0
votes
0 answers

How to remove weak reference from iOS project - AFUrlConnectionOperation.m?

I am using AFNetworking with AFDownloadRequestOperation to do downloads in my iPhone app. I get an error when building: Compile AFUrlConnectionOperation.m The current deployment target does not support automated __weak references In my code in the…
0
votes
1 answer

ListView Loading Images with AsyncTask

I have been reading through a lot of answers of questions that are similar to mine, but still having problem fixing my issue. I have a project that is an RSS Reader that loads in images in the background with an AsyncTask class. The program works,…
bmjohns
  • 6,344
  • 1
  • 33
  • 39
0
votes
1 answer

Can we use self.prefix name for weak properties in ARC

I am working on a ARC based project. I have declared a property for tableView as follows @property (weak, nonatomic) IBOutlet UITableView *logTable. In the ViewDidLoad I do the following - (void)viewDidLoad { [super viewDidLoad]; …
Raj
  • 1,119
  • 1
  • 15
  • 32
0
votes
3 answers

Block and retain cycle can't catch it

I've got an issue with blocks and weak reference, I'm under ARC. I built a class it is a free project that is a kind of easy wrapper around Google Directions API you can download it here:link to the project Im'm using it inside a view controller…
Andrea
  • 26,120
  • 10
  • 85
  • 131
0
votes
3 answers

Strong and weak references in one collection

Suppose the situation in which we must have list of WeakReference and "Strong Reference".But there are not StrongReference class in Java.My solution is Keep list of objects List list =new ArrayList(); and any time when we get…
Ashot
  • 77
  • 5
0
votes
1 answer

Why does my Handler reference a null Activity?

Inside of the handler after I first enter the app from a clean state, the Handler handles the MSG_PULLED action however, the reference to main is null. The weak reference is not null. How can this possibly be happening? Inspired by this post: This…
hunterp
  • 15,716
  • 18
  • 63
  • 115
0
votes
1 answer

"Weak reference" in Linux memory manager?

In Java, a weak reference is garbage collected if memory out. In Linux, malloc() always returns a strong reference, ie. the pointer is never freed until the caller call free() function. I want to allocate a buffer for caching, which could be freed…
Lenik
  • 13,946
  • 17
  • 75
  • 103
0
votes
1 answer

Does this arrangement of objects leak?

I have two classes A and B. In A, I am creating an object of B, and in B I am accessing the back Button property of class A. In B, I have declared A as a weak reference variable. The code runs fine without any crash. However I am not sure if there…
Deep Arora
  • 1,900
  • 2
  • 24
  • 40