Is there a way of totally destroying an object? I mean, if you remove the instance it doesn't really kill it and in my case it's a listener of events so it still reacts upon them. Any ideas? Garbage collector is not fast enough :-(
In the future, please post non-J2EE questions in forum269
In short ... no there isn't - Java does not support destructors or finalizers.
If you have a listener that needs to be shut down, you'll have to code it so that you can either tell the listener to shut down, or for your code that is notified by that listener, to have the ability to remove the listener from their scope.
If your listener still receives events because it's still registered with a 'dispatcher' object, it will never be garbage collected anyway. The dispatcher object clearly still has a reference to it.
As sedj and Dian have said, the correct way to write Java code like this is to manage your objects properly, not worry about how to force the GC to remove objects for you.
If a listener is no longer needed, it should be removed from all the objects it was registered with. If a class has an 'addListener' method, it will usually have a 'removeListener' method as well for just this purpose. If you've written the dispatcher class and it doesn't have such a method then you should add one.
If your listener object is then truly unneeded (you've removed it from all referencing objects - dispatchers and containers etc), then the CG will attend to it in due course as an unreferenced object.
Tim
--------------------------- "Your morbid fear of losing,
destroys the lives you're using." - Ozzy
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.