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

Strange deletion behavior after removing last reference

I have a strange problem while trying to delete the last reference of an object. The code is: import sys import weakref class Ref: def __init__(self, name): self.name = name self.ref = [] def reference(self, obj): …
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
0
votes
0 answers

Delegate of subclassed NSTableView is not retained?

I have a class ETXTableView which is a subclass of NSTableView. (CHANGED after comment) Its delegate is set in IB to the viewController (as is its data source). There are four instances of this class: four tables above each other. The user can click…
Carelinkz
  • 936
  • 8
  • 27
0
votes
2 answers

How can I enforce single ownership with weak_ptr? (Or, how to subclass shared_ptr and override dtor)

So, I've done (a small amount) of reading and am aware that unique_ptr in combination with raw pointers is the pattern to use when modeling unique ownership. However, I really like the simple and clear concept of using a weak_ptr to check if a…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
0
votes
1 answer

Objective-C, ARC: Is it correct to use __weak arguments?

Here's a little example of using weak arguments: @interface MYTestObject : NSObject @end @implementation MYTestObject { void(^_block)(void); } - (void)dealloc { NSLog(@"DEALLOC!"); } - (id)init { if (self = [super init]) { [self…
0
votes
2 answers

ConcurrentHash map with Weak Reference

I am stuck in a problem and really need some help to go through this In my application. I have a ConcurrentHashMap where multiple can thread can store or retrieve data simultaneously. To solve this issue I have chosen. Now the problem after certain…
Souvik
  • 1,219
  • 3
  • 16
  • 38
0
votes
2 answers

LD_PRELOAD and weak references minimal example doesn't work

This is probably going to be embarrassing: I am using library prelaoding in other projects, but I cannot get this minimal example to work: weakref.h: void f_weak() __attribute__((weak)); weakref.c: #include #include "weakref.h" void…
steffen
  • 8,572
  • 11
  • 52
  • 90
0
votes
1 answer

Memoryleak in onPostExecute-method of AsyncTask

I have a memoryleak in my app and according to the debugger in Eclipse it seems to be in the onPostExecute-method in Asynctask. Its all about decoding bitmaps and put those (9) into a scrollview. The scrollView is in turn put into an AlertDialog.…
0
votes
1 answer

memoryleak in asynctask despite using weakreference

I have a memoryleak in my app that I derived to the AsyncTask-class and in the onPostExecute-method. The onPostExcecute-method receives 9 scaleddown bitmaps and put those into a scrollview. The scrollview is in turn put into an alertdialog. Any…
0
votes
1 answer

NullReference at seemingly innocent WeakReference access?

So, I have a piece of code using WeakReferences. I know of the common .IsAlive race condition, so I didn't use that. I basically have something like this: WeakReference lastString=new WeakReference(null); public override string ToString() { …
Earlz
  • 62,085
  • 98
  • 303
  • 499
0
votes
2 answers

Android Context lifecycle

This has been bothering me for a while. Simply put, can activity context be passed around without preventing the OS from destroying it when applicable? Example: An asynchronous request on a socket is made, attached to this is an hashmap where…
0
votes
3 answers

Using WeakReferences in Java

I have been trying to use WeakReference in my Android projects and I have never had any success. Now I really need it as I am dealing with old devices and I have to keep the memory clean as much as I can. Anyway, I have an array which has about 1000…
Mr H
  • 5,254
  • 3
  • 38
  • 43
0
votes
1 answer

dark relation between execution thread and weakreferences?

I'm facing a strange problem I don't know even how to properly explain, but I hope somebody has faced this pseudo-problem and can tell me more or less why it happens. I have a complex activity that occassionally spans threads to load resources. It…
rupps
  • 9,712
  • 4
  • 55
  • 95
0
votes
1 answer

Registering created object in outer @autoreleasepool block and strange __weak pointer behavior

Code: @autoreleasepool { id __autoreleasing obj = nil; @autoreleasepool { obj = [[NSObject alloc] init]; _objc_autoreleasePoolPrint(); } NSLog(@"obj: %@", obj); // Error …
0
votes
0 answers

Why variable with __weak qualifier and __bridge cast retains an object?

I have following code: int main(int argc, char *argv[]) { void *pointer = NULL; @autoreleasepool { NSMutableArray *arr = [[NSMutableArray alloc] init]; pointer = (__bridge_retained void *)arr; NSLog(@"retain…
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
0
votes
1 answer

Weak Reference on Locale in Android

Please check this code: TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Locale loc = null; WeakReference locWeak = new WeakReference(loc); String country = locWeak.get().getDisplayCountry(new…
Mr H
  • 5,254
  • 3
  • 38
  • 43