Article:
 |
|
Seven Low-Cost Ways to Improve Legacy Code
|
| Subject: |
|
Replace Listeners with Weak Listeners |
| Date: |
|
2004-05-06 01:16:20 |
| From: |
|
ipreuss
|
|
|
|
This one can also easily introduce bugs. Consider the following common idiom:
myClass.addFooListener(new FooListener() {
public void fooHappened() {
reactToFoo();
}
});
If MyClass remembers its listeners using weak references, the new listener immediately is easy game for the garbage collector - which certainly is not the intent of the code.
|
Showing messages 1 through 2 of 2.
-
Replace Listeners with Weak Listeners
2004-05-07 15:09:21
Robert Simmons, Jr. |
[View]
-
Replace Listeners with Weak Listeners
2004-05-11 08:48:20
ipreuss
[View]
Second of all, creating objects that you have no direct reference to is an iffy thing at best. This type of code is very prone to memory leaks.