weak-references

WeakValueDictionary retaining reference to object with no more strong references

倖福魔咒の 提交于 2020-01-14 10:09:53
问题 >>> from weakref import WeakValueDictionary >>> class Foo(object): ... pass >>> foo = Foo() >>> db = WeakValueDictionary() >>> db['foo-id'] = foo >>> del foo >>> dict(db) {'foo-id': <__main__.Foo object at 0x4dd946c>} Why does it show this instead of an empty dictionary? Note that this code produces the result I'd expect: >>> db2 = WeakValueDictionary() >>> db2['disposable-id'] = Foo() >>> dict(db2) {} It also behaves as expected when executing a script (instead of the interactive interpreter

Weak reference with a condition / timeout

戏子无情 提交于 2020-01-14 08:10:50
问题 is it possible in Java to create a weak reference whose object can be sent to garbage collector only if a specified condition returns true ? Let's say that I have something like a cache which maps ID numbers to some data: Map<Integer, SomeData> cache = new HashMap<>(); SomeData have two important methods - void updateTime() , which just sets an internal variable to current time, and boolean canBeDeleted() , which checks if the object have or haven't been used in last 10 minutes (simply by

Weak reference with a condition / timeout

不问归期 提交于 2020-01-14 08:10:06
问题 is it possible in Java to create a weak reference whose object can be sent to garbage collector only if a specified condition returns true ? Let's say that I have something like a cache which maps ID numbers to some data: Map<Integer, SomeData> cache = new HashMap<>(); SomeData have two important methods - void updateTime() , which just sets an internal variable to current time, and boolean canBeDeleted() , which checks if the object have or haven't been used in last 10 minutes (simply by

Do I need to keep a strong reference to a WeakReference that is only used to perform finalization?

柔情痞子 提交于 2020-01-14 04:31:11
问题 I'd like to use a WeakReference as a more efficient finalize() method, for the purpose of freeing native resources associated with an object as soon as it becomes possible to do so, without using finalization (which has significantly higher costs than using a WeakReference). Since this is the only purpose of the WeakReference (I will never use the WeakReference to obtain the referenced object), it seems wasteful to take the time and space to maintain a list of my WeakReferences to prevent

Clarifications regarding weak references in actionscript listeners

旧时模样 提交于 2020-01-11 12:36:11
问题 I understand how weak references work, but I am bit confused regarding it's use in actionscript event listeners. Consider the example below: public class Rectangle extends MovieClip { public function Rectangle() { var screen:Shape=new Shape(); screen.addEventListener(MouseEvent.MOUSE_OUT, new Foo().listen, false, 0, true); addChild(screen); } } public class Foo extends MovieClip { public function listen(e:MouseEvent):void { trace("tracing"); } } Now here, since there is only a weak reference

If a child object is released in the dealloc of a parent object, why would the child object's weak reference to the parent be nil*

独自空忆成欢 提交于 2020-01-07 09:09:07
问题 *because the child is released within the dealloc of the parent, wouldn't that imply that the parent object still exists, and that the child's weak reference back to the parent should still be valid? I have a parent object, a NSViewController subclass, which owns another object, a NSWindowController subclass. @interface MyViewController : NSViewController @property (strong) MyWindowController *myWindowController; The NSWindowController subclass has a weak reference back to the

If a child object is released in the dealloc of a parent object, why would the child object's weak reference to the parent be nil*

房东的猫 提交于 2020-01-07 09:09:07
问题 *because the child is released within the dealloc of the parent, wouldn't that imply that the parent object still exists, and that the child's weak reference back to the parent should still be valid? I have a parent object, a NSViewController subclass, which owns another object, a NSWindowController subclass. @interface MyViewController : NSViewController @property (strong) MyWindowController *myWindowController; The NSWindowController subclass has a weak reference back to the

Block and retain cycle can't catch it

瘦欲@ 提交于 2020-01-06 19:03:44
问题 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 the problem is that after using it the view controller is not deallocated. I guess that is an issue with this object because if I comment it out or set to nil everything works correctly. I'm not able to understand where is the retain cycle, of course

Block and retain cycle can't catch it

懵懂的女人 提交于 2020-01-06 19:03:07
问题 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 the problem is that after using it the view controller is not deallocated. I guess that is an issue with this object because if I comment it out or set to nil everything works correctly. I'm not able to understand where is the retain cycle, of course

Crash with iAd only on iOS 7 - weak delegate is bad pointer

一世执手 提交于 2020-01-06 14:54:45
问题 I'm getting a strange crash with the iAd error callback. Here is my crash log: Exception Type: EXC_BAD_ACCESS Code: KERN_INVALID_ADDRESS at 0x2281 0com.apple.main-thread Crashed 0 libobjc.A.dylib objc_retain + 17 1 YouDoodle AdView.m line 265 -[AdContainerView requestGAD] 2 iAd -[ADBannerView _forwardErrorToDelegate:] + 254 3 iAd -[ADBannerView serverBannerViewDidFailToReceiveAdWithError:] + 178 4 iAd -[ADAdSpace setServiceAdSpace:] + 472 5 CoreFoundation __CFNOTIFICATIONCENTER_IS_CALLING_OUT