java.nonstandard.WeakRef

Creates a weak reference to a class instance.

A weak reference lets you hold onto a pointer to an object without preventing the garbage collector from collecting it. If the garbage collector collects the object then the weak pointer will become 'null'. Thus one should always check a weak pointer for null before doing anything that depends upon it having a value.

Tested with: DMD 1.025 / Phobos 1.025 DMD 1.025 / Tango 0.99.4

Usage example:

1 class Something {}
2 
3 auto a = new Something();
4 auto wa = new WeakRef!(Something)(a);
5 std.gc.fullCollect();
6 
7 // Reference 'a' prevents collection so wa.ptr is non-null
8 assert(wa.ptr is a);
9 
10 delete a;
11 
12 // 'a' is gone now, so wa.ptr magically becomes null
13 assert(wa.ptr is null);

Meta

Authors

William V. Baxter III Contributors:

Date

Date: 21 Jan 2008

License

Public Domain where allowed by law, ZLIB/PNG otherwise.