weak-references

Python: which types support weak references?

老子叫甜甜 提交于 2020-01-01 04:23:05
问题 Code: from weakref import WeakSet se = WeakSet() se.add(1) Output: TypeError: cannot create weak reference to 'int' object Doc: Several built-in types such as list and dict do not directly support weak references but can add support through subclassing: ... Other built-in types such as tuple and int do not support weak references even when subclassed (This is an implementation detail and may be different across various Python implementations.). This isn't expressive enough to explain: Why

I have a circular reference. How can I create a weak reference in Objective-C?

依然范特西╮ 提交于 2020-01-01 03:58:29
问题 I'm working on an iPhone application. I have an object of class Row that needs to release numerous objects of the class Block . Every Block currently has a property that retains an instance variable of class Row . @interface Block : UIImageView { Row *yCoord; } @property (nonatomic,retain) Row *yCoord; @end Every Row contains an NSMutableArray of these Blocks. @interface Row : NSObject { NSMutableArray *blocks; } -(void)addBlock:(Block*)aBlock; @end @implementation Row -(void)addBlock:(Block*

Why are weak pointers useful?

你离开我真会死。 提交于 2019-12-31 08:38:05
问题 I've been reading up on garbage collection looking for features to include in my programming language and I came across "weak pointers". From here: Weak pointers are like pointers, except that references from weak pointers do not prevent garbage collection, and weak pointers must have their validity checked before they are used. Weak pointers interact with the garbage collector because the memory to which they refer may in fact still be valid, but containing a different object than it did

How to set CADisplayLink in Swift with weak reference between target and CADisplayLink instance

元气小坏坏 提交于 2019-12-31 02:43:08
问题 In Objective-C, we can init CADisplayLink with Proxy Pattern to break strong reference: WeakProxy *weakProxy = [WeakProxy weakProxyForObject:self]; self.displayLink = [CADisplayLink displayLinkWithTarget:weakProxy selector:@selector(displayDidRefresh:)]; Then, just invalidate the displayLink in dealloc : - (void)dealloc { [_displayLink invalidate]; } However, NSProxy seems can't be inherited in Swift: https://bugs.swift.org/browse/SR-1715 I tried to write like this: weak var weakSelf = self

Is there a way to do a WeakList or WeakCollection (like WeakReference) in CLR?

笑着哭i 提交于 2019-12-30 16:24:01
问题 Using a List<WeakReference> will not work as I want. What I want is for WeakReferences to be automatically removed from the list whenever the object they reference is garbage collected. ConditionalWeakTable<TKey,TValue> does not satisfy me either, because although its keys and values are weakly referenced and collectable, you cannot enumerate them! 回答1: I agree that implementing a WeakList<T> is possible, but I don't think it's exactly easy . You're welcome to use my implementation here. The

Can I hook when a weakly-referenced object (of arbitrary type) is freed?

筅森魡賤 提交于 2019-12-30 07:31:56
问题 I'm writing a container class in Swift, which works like as java.util.WeakHashMap in Java. My current implementation is here. class WeakRefMap<Key: Hashable, Value: AnyObject> { private var mapping = [Key: WeakBox<Value>]() subscript(key: Key) -> Value? { get { return mapping[key]?.raw } set { if let o = newValue { mapping[key] = WeakBox(o) } else { mapping.removeValueForKey(key) } } } var count: Int { return mapping.count } } class WeakBox<E: AnyObject> { weak var raw: E! init( _ raw: E) {

Pickling weakref in Python

我的未来我决定 提交于 2019-12-30 06:31:08
问题 I am still pretty new to Python and even newer to pickling. I have a class Vertex(ScatterLayout) with a __getnewargs__(): def __getnewargs__(self): return (self.pos, self.size, self.idea.text) My understanding is that this will cause the pickle to pickle the object from __getnewargs__() rather than the object's dictionary. The pickle is called in the following method (in a different class MindMapApp(App) ): def save(self): vertices = self.mindmap.get_vertices() edges = self.mindmap.get_edges(

Pickling weakref in Python

China☆狼群 提交于 2019-12-30 06:30:27
问题 I am still pretty new to Python and even newer to pickling. I have a class Vertex(ScatterLayout) with a __getnewargs__(): def __getnewargs__(self): return (self.pos, self.size, self.idea.text) My understanding is that this will cause the pickle to pickle the object from __getnewargs__() rather than the object's dictionary. The pickle is called in the following method (in a different class MindMapApp(App) ): def save(self): vertices = self.mindmap.get_vertices() edges = self.mindmap.get_edges(

Weak property is set to nil in dealloc but property's ivar is not nil

橙三吉。 提交于 2019-12-28 05:35:11
问题 I noticed the following in Objective-C with ARC enabled: Let's have simple class A and autosynthesized weak property @interface A @property (nonatomic, weak) id refObject; @end @implementation A @end And second class B with dealloc implemented @interface B @end @implementation B -(void) dealloc { NSLog(@"In dealloc"); } @end And finally somewhere in class A have the following: @implementation A ... -(void) foo { B* b = [B new]; self.refObject = b; // Just use b after the weak assignment // in

How does weak_ptr work?

时间秒杀一切 提交于 2019-12-28 02:26:26
问题 I understand how to use weak_ptr and shared_ptr . I understand how shared_ptr works, by counting the number of references in its object. How does weak_ptr work? I tried reading through the boost source code, and I'm not familiar enough with boost to understand all the things it uses. Thanks. 回答1: shared_ptr uses an extra "counter" object (aka. "shared count" or "control block") to store the reference count. (BTW: that "counter" object also stores the deleter.) Every shared_ptr and weak_ptr