weak-references

Knowing where retain cycles are and removing them

女生的网名这么多〃 提交于 2019-11-29 12:52:51
I was wondering if there was an easy way (or at least a way) to find out where retain cycles exist in your program. Also, if I then know where these retain cycles exist, depending on their types (e.g. variable or closure), how do I make them weak. I need to stop all retain cycles with self (my GameScene) so that it deallocates when I don't need it anymore and I want to restart it. Any tips, advice, answers, and feedback would be greatly appreciated (and providing specific code and examples would be preferred). Thank you. Edit: @Sweeper's answer was just what I was looking for. If you're having

WeakReference string didn't garbage collected? How?

感情迁移 提交于 2019-11-29 08:06:05
I'm reading about WeakReference in wikipedia and I saw this code public class ReferenceTest { public static void main(String[] args) throws InterruptedException { WeakReference r = new WeakReference(new String("I'm here")); WeakReference sr = new WeakReference("I'm here"); System.out.println("before gc: r=" + r.get() + ", static=" + sr.get()); System.gc(); Thread.sleep(100); // only r.get() becomes null System.out.println("after gc: r=" + r.get() + ", static=" + sr.get()); } } When It runs this is the result before gc: r=I'm here, static=I'm here after gc: r=null, static=I'm here sr and r

C# WeakReference object is NULL in finalizer although still strongly referenced

时间秒杀一切 提交于 2019-11-29 05:11:06
Hi I have code here where I don't understand why I hit the breakpoint (see comment). Is this a Microsoft bug of something I don't know or I don't understand properly ? The code was tested in Debug but I think it should not changes anything. Note: You can test the code directly in a console app. JUST FOR INFORMATION... following supercat answer, I fixed my code with proposed solution and it works nicely :-) !!! The bad thing is the usage of a static dict and the performance the goes with it but it works. ... After few minutes, I realized that SuperCat give me all hints to do it better, to

Garbage Collection should have removed object but WeakReference.IsAlive still returning true

落花浮王杯 提交于 2019-11-29 03:48:34
I have a test that I expected to pass but the behavior of the Garbage Collector is not as I presumed: [Test] public void WeakReferenceTest2() { var obj = new object(); var wRef = new WeakReference(obj); wRef.IsAlive.Should().BeTrue(); //passes GC.Collect(); wRef.IsAlive.Should().BeTrue(); //passes obj = null; GC.Collect(); wRef.IsAlive.Should().BeFalse(); //fails } In this example the obj object should be GC'd and therefore I would expect the WeakReference.IsAlive property to return false . It seems that because the obj variable was declared in the same scope as the GC.Collect it is not being

Weak object in an NSDictionary?

杀马特。学长 韩版系。学妹 提交于 2019-11-29 02:48:53
问题 I would like to store a zeroing weak reference to an object in a NSDictionary . This is for a reference to a parent NSDictionary , so I can crawl back up a large structure without searching. I can not use __weak here; even if my local reference is weak, the NSDictionary will store a strong reference to the object that was weakly referenced. And, of course, NSDictionary can't have nil objects. I'm on iOS, not Mac, so NSHashTable isn't available. And I only want one object to be weak; the rest

Swift Closures - Capturing self as weak

我怕爱的太早我们不能终老 提交于 2019-11-29 02:48:31
问题 I am trying to resolve a closure based strong reference cycle in Swift. In the code below, object is retained by the owning view controller. ProgressHUD is a UIView that's also retained by the owning view controller. ProgressHUD is leaked every time the completion handler is called. When using the new closure capture feature, declaring self as weak or unowned does not resolve the memory leak. object.setCompletionHandler { [weak self] (error) -> Void in if(!error){ self?.tableView.reloadData()

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

喜夏-厌秋 提交于 2019-11-29 02:07:41
If I have a closure passed to a function like this: someFunctionWithTrailingClosure { [weak self] in anotherFunctionWithTrailingClosure { [weak self] in self?.doSomething() } } If I declare self as [weak self] in someFunctionWithTrailingClosure 's capture list without redeclaring it as weak again in the capture list of anotherFunctionWithTrailingClosure self is already becoming an Optional type but is it also becoming a weak reference as well? Thanks! The [weak self] in anotherFunctionWithTrailingClosure is not needed. You can empirically test this: class Experiment { func

How do weak and strong references look like in objective-c?

我怕爱的太早我们不能终老 提交于 2019-11-29 00:40:02
问题 Wikipedia states "In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector". How do those two types of references look like in code? Does a weak reference is a reference made by an autoreleased message? 回答1: The following answer is for the case when there is no garbage collection (such as on iOS). In the case of garbage collection, there is actually a keyword ( __weak ) to create a weak reference. A "weak"

Good implementation of weak dictionary in .Net

徘徊边缘 提交于 2019-11-28 22:42:34
Where can I find good implementation of IDictionary which uses weak references inside? Dictionary should be holding only weak references to values and eventually clean up itself of dead references. Or should I just write it myself? ConditionalWeakTable Class uses weak keys and automatically removes the key/value entry as soon as no other references to a key exist outside the table. Paul Alexander You'll need to write it yourself. It should be relatively straight forward, implementing the IDictionary interface and then storing the actual values as WeakReferences. You can then check the values

Using objc_setAssociatedObject with weak references

只谈情不闲聊 提交于 2019-11-28 21:57:48
问题 I know that OBJC_ASSOCIATION_ASSIGN exists, but does it zero the reference if the target object is dealloced? Or is it like the old days where that reference needs to get nil-ed or we risk a bad access later on? 回答1: As ultramiraculous demonstrated, OBJC_ASSOCIATION_ASSIGN does not do zeroing weak reference and you risk to access a deallocated object. But it’s quite easy to implement yourself. You just need a simple class to wrap an object with a weak reference: @interface WeakObjectContainer