[Newbies] What is a "weak" reference

Bert Freudenberg bert at freudenbergs.de
Tue Jul 15 13:55:57 UTC 2008


Am 15.07.2008 um 06:42 schrieb Marcin Tustin:

> One of the uses of weak references in other languages is to keep a  
> collection of all the instances of a class without preventing them  
> being garbage collected. Obviously, this is not necessary in squeak.


One major use in Squeak is for ensuring that system resources (such as  
file handles) are released when a file object is garbage-collected.  
Note this is only a fall-back, you should still use #close on files,  
but to prevent resource leakage in odd cases, a weak reference is held  
to each open file.

It's one of the low-level implementation details that exist to make  
the normal live of a Smalltalk programmer simpler. Only the framework  
designer interfacing to non-Smalltalk objects (such as files) needs to  
worry about it. I've never seen a need for weak references in  
application-level code.

- Bert -




More information about the Beginners mailing list