weak-references

Rationale for Soft-/Weak-/PhantomReferences clearing references to objects which have reference to tracked object

徘徊边缘 提交于 2019-12-11 06:15:58
问题 The documentation for Soft-, Weak- and PhantomReferences all include a line simiar to the following (taken from PhantomReference ): At that time it will atomically clear all phantom references to that object and all phantom references to any other phantom-reachable objects from which that object is reachable. The part which is confusing me is the one about the other phantom-reachable objects. If I understand it correctly this describes this case: Objects: A B References: -> : Strong reference

weak variable is intermediately nil

*爱你&永不变心* 提交于 2019-12-10 20:11:06
问题 when are weak variable gets nil? weak var backgroundNode = SKSpriteNode(texture: SKTexture(image: initialBackgroundImage!)) backgroundNode!.position = CGPoint(x: rootSkScene!.frame.midX, y: rootSkScene!.frame.midY) getting always: fatal error: unexpectedly found nil while unwrapping an Optional value Xcode error 回答1: So weak variable means that this reference isn't increasing counter of references by one. As you know if counter equals zero object will be deleted. What you are doing here that

How can you use weak references in Swift generic data structure typed as a protocol?

隐身守侯 提交于 2019-12-10 17:42:35
问题 I am wanting to use weak references in generic data structures; in the example below an Array, but in general any generic type. I can almost get it to work :( My experiments started off well; the following works: // Array of weak references OK struct WeakReference<T: AnyObject> { weak var value: T? } class C { var i: Int = 0 } let c = C() // Strong reference to prevent collection let weakCs = [WeakReference(value: c)] // OK print("C: \(weakCs[0].value!.i)") // 0 I can add a protocol: // Array

Iterate array of weak references where objects conform to a protocol in Swift

那年仲夏 提交于 2019-12-10 15:16:37
问题 I want to store objects in an array, where objects are weak, and conforms to a protocol. But when I try to loop it, I get a compiler error: public class Weak<T: AnyObject> { public weak var value : T? public init (value: T) { self.value = value } } public protocol ClassWithReloadFRC: class { func reloadFRC() } public var objectWithReloadFRC = [Weak<ClassWithReloadFRC>]() for owrfrc in objectWithReloadFRC { //If I comment this line here, it will able to compile. //if not I get error see below

Inherited WeakReference throwing ReflectionTypeLoadException in Silverlight

牧云@^-^@ 提交于 2019-12-10 14:42:52
问题 I'm trying to use a type-safe WeakReference in my Silverlight app. I'm following the recipe on this site: http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html only using the System.WeakReference and omitting the stuff that references Serialization. It's throwing a ReflectionTypeLoadException when I try to run it, with this message: "{System.TypeLoadException: Inheritance security rules violated while overriding member: 'Coatue.Silverlight.Shared.Cache.WeakReference`1..ctor()'

How to zeroing weak references under non-ARC?

孤街醉人 提交于 2019-12-10 13:57:09
问题 I don't like ARC. But the most important feature of ARC, zeroing weak reference, is missing under non-ARC. Currently I'm using MAZeroingWeakRef, it works, but hacky, sometimes makes codes redundant. Any other ways for zeroing weak references? 回答1: Implementing zeroing weak reference is not hard. All what you have to do is just tracking all referencing pointers - store them in a collection - and assigning NULL when pointing object is being deallocated. Anyway, doing all these things manually

What happens to a WeakReference after GC of WeakReference.Target

泄露秘密 提交于 2019-12-10 12:48:03
问题 What happens to the WeakReference when the target object referenced by WeakReference.Target has been garbage collected? Does the WeakRerence stay alive and keeps existing? The reason why I am asking is that I have a list of WeakReferences stored in a List. During runtime new WeakReferences constantly are getting added to that list. Now when the target object dies, do I have to cleanup the abandoned WeakReference myself? If so, is there a clever trick how I could do this? Can I get notified

Clarification about weak references and a retain cycles

自作多情 提交于 2019-12-10 09:30:44
问题 I have the following code: AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request]; operation.completionBlock = ^{ if([operation hasAcceptableStatusCode]){ } }; ARC doesn't seem to like [operation hasAcceptableStatusCode], and i get the following warning: "Capturing 'operation' strongly in this block is likely to lead to a retain cycle". I'm not very experienced with referencing, any idea whats the way to go here? Thanks, Shai 回答1: Blocks capture (retain)

Creating a deepcopy of class instance with nested weakref to it

ぐ巨炮叔叔 提交于 2019-12-10 09:30:19
问题 I have two classes: a parent class and a container class. The parent class instance has matching container class instance as a weak reference. There is a problem while deep copying the parent instance, the weakref is still linking to the original instance. Here is a minimal example: import weakref from copy import deepcopy class Container: def __init__(self, parent): self.parent = weakref.ref(parent) class Parent: def __init__(self): self.container = Container(self) if __name__ == '__main__':

How to remove a weakReference from a list?

前提是你 提交于 2019-12-10 03:36:48
问题 I've got a list of weakReferences to objects in java. How do i write a method that gets the real object instance and removes it's weak reference from this list? thanks. 回答1: It's not entirely clear what you mean, but I think you may want: public static <T> void removeReference(List<WeakReference<T>> list, T reference) { for (Iterator<WeakReference<T>> iterator = list.iterator(); iterator.hasNext(); ) { WeakReference<T> weakRef = iterator.next(); if (weakRef.get() == reference) { iterator