Simon TRENY, el 29 de marzo a las 16:33 me escribiste: > Hello, > > I have a class "A" and I'd like to keep a list of all the created > instances of this class. To do that, I have a static List!(A) in the > A class and, in the constructor, I add each new instance to this list. > This gives me the following code: > > class A { > private static List!(A) s_instances; > > public this() { > s_instances.add(this); > } > > public ~this() { > s_instances.remove(this); > } > > public static void printAll() { > foreach (A instance; s_instances) > print(instance.toString()); > } > } > > But then, since all the instances are referenced by the static list, > they are never garbage-collected, which could be a problem. In some > other languages, this can be solved using weak references, but I haven't > found any informations about using weak references in D. Is there any > way to solve this problem?
This was discussed several times in the past. For example: http://www.digitalmars.com/d/archives/digitalmars/D/learn/weak_references_13301.html http://www.digitalmars.com/d/archives/digitalmars/D/learn/Soft_weak_references_8264.html http://www.digitalmars.com/d/archives/digitalmars/D/announce/ANN_WeakObjectReference_-_class_to_hold_weak_references_9103.html etc. I hope it helps. -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- Que el viento y la lluvia sean dos hermanos y corran furiosos por los terraplenes de Víctor Heredia. -- Ricardo Vaporeso. Lanús, 1912.