weak-references

Other uses of weak references?

北城余情 提交于 2019-12-05 23:19:54
问题 I know that weak references are a good candidate for memoizing potentially large sets of data, and Wikipedia's article on weak references only lists "keeping track of the current variables being referenced in the application" and the statement "Another use of weak references is in writing a cache". What are some other situations (more specific than just "caching results") where the use of weak references is A Good Idea TM ? 回答1: In Flex, use weak references to avoid memory leaks. If an event

Playing around with ARC: Force release irritation?

点点圈 提交于 2019-12-05 21:27:28
I am currently playing around with ARC a bit to get some things figured out, before starting to do the actual work. I did setup this code: NSNumber* n = [[NSNumber alloc] initWithInt:3]; __weak NSNumber* weakN = n; n = nil; NSLog(@">>>: %@ %@", n, weakN); I expected n and weakN to be nil, as n = nil; should trigger a release in my eyes? Unfortunately it doesn't. The output is ">>>: (null) 3". What am I missing here? Another thing is, that I am pretty sure, the below code was giving me a hard time when starting with arc: __weak NSNumber* weakN2 = [[NSNumber alloc] initWithInt:3]; NSLog(@">>>: %

Memory Leak and Weak References

£可爱£侵袭症+ 提交于 2019-12-05 19:08:34
I'm having an issue which looks like a memory leak in one of my applications (the application uses more memory over time, an after about a week of work it hangs). I've detected and fixed some leaks related to classes I've written (comparing heap dumps taken with sos.dll revealed them quickly), and those no longer increase in number. Currently, the only thing that dramatically increases over time are WeakReference instances, which increase at a steady rate of 1,000 new WeakReference instances per minute. My code doesn't use WeakReference directly, I never create those myself. What could cause

Creating a deepcopy of class instance with nested weakref to it

空扰寡人 提交于 2019-12-05 16:20:42
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__': parent1 = Parent() assert(parent1 is parent1.container.parent()) parent2 = deepcopy(parent1) assert

bad_weak_ptr when calling shared_from_this() in base class

可紊 提交于 2019-12-05 10:47:04
I have a SuperParent class, a Parent class (derived from SuperParent ) and both contain a shared_ptr to a Child class (which contains a weak_ptr to a SuperParent ). Unfortunately, I'm getting a bad_weak_ptr exception when trying to set the Child 's pointer. The code is the following: #include <boost/enable_shared_from_this.hpp> #include <boost/make_shared.hpp> #include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> using namespace boost; class SuperParent; class Child { public: void SetParent(shared_ptr<SuperParent> parent) { parent_ = parent; } private: weak_ptr<SuperParent> parent_; };

Bug in WeakAction in case of Closure Action

会有一股神秘感。 提交于 2019-12-05 10:33:01
问题 In one of the projects I take part in there is a vast use of WeakAction . That's a class that allows to keep reference to an action instance without causing its target not be garbage collected. The way it works is simple, it takes an action on the constructor, and keeps a weak reference to the action's target and to the Method but discards the reference to the action itself. When the time comes to execute the action, it checks if the target is still alive, and if so, invokes the method on the

Equivalent to SoftReference in .net?

不打扰是莪最后的温柔 提交于 2019-12-05 09:01:10
I am familiar with WeakReference , but I am looking for a reference type that is cleared only when memory is low, not simply every time when the gc runs (just like Java's SoftReference ). I'm looking for a way to implement a memory-sensitive cache. Andrew Hare No there isn't an equivalent. Is there a particular reason why WeakReference won't do the job? Here is a similar question to yours: Why doesn't .NET have a SoftReference as well as a WeakReference, like Java? Maybe the ASP.NET Cache class ( System.Web.Caching.Cache ) might help achieve what you want? It automatically remove objects if

Weak hashmap with weak references to the values?

被刻印的时光 ゝ 提交于 2019-12-05 03:45:31
I am building an android app where each entity has a bitmap that represents its sprite. However, each entity can be be duplicated (there might be 3 copies of entity asdf for example). One approach is to load all the sprites upfront, and then put the correct sprite in the constructors of the entities. However, I want to decode the bitmaps lazily, so that the constructors of the entities will decode the bitmaps. The only problem with this is that duplicated entities will load the same bitmap twice, using 2x the memory (Or n times if the entity is created n times). To fix this, I built a

How to remove a weakReference from a list?

做~自己de王妃 提交于 2019-12-05 03:23:19
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. 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.remove(); } } } Have a look at the Javadocs for WeakReference . Two important things to note: 1. it is protected

Iterating a WeakHashMap

邮差的信 提交于 2019-12-05 01:26:09
I'm using a WeakHashMap concurrently. I want to achieve fine-grained locking based on an Integer parameter; if thread A needs to modify a resource identified by Integer a and thread B does the same for resource identified by Integer b , then they need not to be synchronized. However, if there are two threads using the same resource, say thread C is also using a resource identified by Integer a , then of course thread A and C need to synchronize on the same Lock. When there are no more threads that need the resource with ID X then the Lock in the Map for key=X can be removed. However, another