weak-references

Memory Leak and Weak References

百般思念 提交于 2019-12-07 14:49:52
问题 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

Why __weak object will be added to autorelease pool?

筅森魡賤 提交于 2019-12-07 14:39:21
问题 id __weak obj1 = obj0; equals id __weak obj1 = obj0; id __autoreleasing tmp = obj0; In Pro multithreading and memory management for iOS and OSX . But why the obj1 need to add to the autorelease pool, I think making a weak pointer of an object should not affect its lifetime. 回答1: { NSObject* sp = [NSObject new]; NSObject* __weak wp = sp; } the above code is translate to: id sp = objc_msgSend(NSObject, "new"); id wp; objc_initWeak(&wp, sp); objc_destroyWeak(&wp); objc_storeStrong(&sp, 0); 1)

Objective C - Self Zeroing weak pointer unexpected behaviour

走远了吗. 提交于 2019-12-07 13:00:09
问题 I have recently upgraded from Mavericks to Yosemite and now my unit tests are failing. The problem boiled down to a typo in a weak pointer to string content. Please see the following sample code: NSString* value1; NSString* value2; __weak NSString* weakValue1; __weak NSString* weakValue2; NSMutableString* resultText = [NSMutableString new]; @autoreleasepool { value1 = [NSString stringWithFormat: @"Hello: %d", 1]; value2 = [NSString stringWithFormat: @"Hello %d", 2]; weakValue1 = value1;

C# language: why WeakReference or Weak Event Pattern?

烂漫一生 提交于 2019-12-07 08:32:25
I'm reading "The C# Language", 4th edition, it talks about WeakReference and Weak Event Pattern : CHRISTIAN NAGEL: Memory leaks often result from wrong usage of events. If client objects attach to events but do not detach from them, and the reference to the client object is no longer used, the client object still cannot be reclaimed by the garbage collector because the reference by the publisher remains. This can be avoided by (1) detaching of events when the client object is no longer used, (2) a custom implementation of the add and remove accessors using the WeakReference class holding the

weak references in .net

余生长醉 提交于 2019-12-07 07:56:53
问题 Can someone give some examples of using weak references in .net projects ? 回答1: Think about cache with 2 levels. So objects in the 1st level are referenced with normal references and in then 2nd level with weak references. Thus if you want to expiry your object from the 1st level you can put it in the 2nd level. Next time, when client tries to access this object if you have enough memory object will be promoted from the 2nd level, however if memory is not enough and object was collected you

Equivalent to SoftReference in .net?

青春壹個敷衍的年華 提交于 2019-12-07 05:42:14
问题 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. 回答1: 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? 回答2: Maybe the ASP.NET Cache class

bad_weak_ptr when calling shared_from_this() in base class

ぐ巨炮叔叔 提交于 2019-12-07 05:35:58
问题 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

Weak hashmap with weak references to the values?

流过昼夜 提交于 2019-12-06 21:15:27
问题 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

Iterating a WeakHashMap

喜你入骨 提交于 2019-12-06 20:14:52
问题 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

self refrence inside swift closure return nil some time

流过昼夜 提交于 2019-12-06 15:33:52
问题 I am accessing instance method inside closure in swift, self reference become nil in some cases which result crash my program. I tried to access using [weak self] but it failed to call the instance method when self is nil . [weak self] () -> () in 回答1: The whole point of [weak self] is to not create a reference to self (probably to avoid circular links and memory leaks) so that it can be released. If that happens, then self will be nil . You should either not use [weak self] or, better yet