weak-references

Android Weak Reference of Inner Class

你。 提交于 2019-12-05 01:00:48
问题 I have gone through the article http://developer.android.com/resources/articles/avoiding-memory-leaks.html . In this article it is suggested to use static inner class with Weak Reference . public class GalleryVideo extends Activity { private int AUDIO_NO = 1; ........................... ................ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); gallery = (Gallery) findViewById(R.id.examplegallery); gallery.setAdapter(new AddImgAdp(this));

Can a conforming C# compiler optimize away a local (but unused) variable if it is the only strong reference to an object?

爷,独闯天下 提交于 2019-12-05 00:57:19
See also these related resources: Does the .NET garbage collector perform predictive analysis of code? (on Stack Overflow) WP7: When does GC Consider a Local Variable as Garbage (blog article on MSDN) In other words: Can an object referenced by a local variable be reclaimed before the variable goes out of scope (eg. because the variable is assigned, but then not used again), or is that object guaranteed to be ineligible for garbage collection until the variable goes out of scope? Let me explain: void Case_1() { var weakRef = new WeakReference(new object()); GC.Collect(); // <-- doesn't have to

Should adapters in Android be static inner classes or non-static inner classes

醉酒当歌 提交于 2019-12-05 00:31:44
I have a ListView in an Activity and I am setting a custom adapter to the ListView. Should my adapter class be: private static class MyAdapter extends ArrayAdapter or private class MyAdapter extends ArrayAdapter I guess it should make no difference as long as the adapter is enclosed within the activity reference but wanted to confirm that. Holding onto the context is fine from within an adapter if you are careful about how you use the adapter. Adapters are usually tied to the lifecycle of their Context (an Activity) so it's fine. Use a WeakReference only if it makes sense. 来源: https:/

WeakReference understanding

帅比萌擦擦* 提交于 2019-12-04 23:18:55
I want to create the dictionary of all the ViewModels. public static Dictionary<string, WeakReference> vmCollection = new Dictionary<string, WeakReference>(); Adding it like this vmCollection.Add(name, new WeakReference(viewModel)); And calling the required method like this.. ((vmCollection[viewModel].Target) as BaseViewModel).NewMessage(message); Do I need maintain it as a WeakReference ? What could be the consequences if I don't maintain it as a WeakReference . The only consequence of not using a WeakReference is that the reference in your dictionary will prevent the View Model instances

ListView Loading Images with AsyncTask

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 22:22:55
I have been reading through a lot of answers of questions that are similar to mine, but still having problem fixing my issue. I have a project that is an RSS Reader that loads in images in the background with an AsyncTask class. The program works, except if the user scrolls quickly then the images sometimes do not load in my rows. They never load in the incorrect spot, it just seems like they are skipped if the user scrolls quickly. Also, on start-up, only 2 or 1 of the images in my listview load out of the 4 rows that the user can see. I know the problem has something to do with the

When to use Weak and Phantom references in Java

让人想犯罪 __ 提交于 2019-12-04 21:14:12
问题 I read many articles, but I don't understand - where do I need to use Weak and Phantom references in practice? Soft references - is a good choice for cache, as I understand. But weak and phantom, I don't know when to use. Please provide examples of real tasks where we need to use them. 回答1: You can use weak references for cache, simply like soft references as you said. What good are PhantomReferences? I'm only aware of two serious cases for them: first, they allow you to determine exactly

self refrence inside swift closure return nil some time

元气小坏坏 提交于 2019-12-04 19:48:14
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 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 probably, be prepared to handle the case of self having been released and set to nil . guard let strong = self

Two weak variables referencing each other in Swift?

六眼飞鱼酱① 提交于 2019-12-04 17:55:11
问题 I'm making another attempt today to try to understand retain cycles and weak references in Swift. Reading through the documentation, I saw the following code example where one of the referencing variables is marked as weak to prevent a retain cycle: class Person { let name: String init(name: String) { self.name = name } var apartment: Apartment? deinit { print("\(name) is being deinitialized") } } class Apartment { let unit: String init(unit: String) { self.unit = unit } weak var tenant:

Can `weakref` callbacks replace `__del__`?

落花浮王杯 提交于 2019-12-04 16:11:37
问题 Is there any obstacle that prevents weakref from doing everything that __del__ does but with much stronger guarantees (e.g., finalize guarantees that the call will be made before the interpreter exits, and the order of calls is well-defined, etc.)? It seems that in the distant past it was thought that weakref would eventually lead to the removal of __del__ from the language. What prevented this from happening? There seems to be few use cases for __del__, and all the ones I'm aware of seem to

Why WeakReferences in RelayCommand?

落花浮王杯 提交于 2019-12-04 15:27:24
I recently upgraded from MVVMLight 3 to 4, and noticed that my Commands broke. It turns out that the use of weak references in the new RelayCommand (implemented in version 3.5) were causing a code construct that I am using to fail. I know that there is some argument for weak refs relating to memory leak, I just don't understand it. This fails: private void InitCommand() { Command = new SwitchMainGridCommand<SwitchMainGridToolViewModel>(this).Command; } By fails, I mean that when I go to use the Command property that I had initialized and bound to, its backing methods have been garbage