Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java garbage collection confusing me

Started by Andybean9 Jul 8, 2006 at 11:17 PM 7 replies 1k views
Original Post
Andybean9
Andybean9
Hi everyone, I have some objects that implement the mouse listener and I was wondering if the listener is not removed will that object ever go away? I have objects that are not referenced anywhere but still get called on mouse events. If this is the case I probably have the write a close function that will remove the listener. Thanks for any help!
Luckless
Luckless
if you have no refereces at all, how is the listener calling it?

Have some code examples?
Old Username: Talroth
If your signature on a web forum takes up more space than your average post, then you are doing things wrong.
Addictman
Addictman
Do the object share a common MouseListener instance, or do they implement it directly? If the latter, it is weird.

Edit: Sorry, posted before I noticed the response.
Development blog, The Omega Sector -- http://www.omegasector.org
Andybean9
Andybean9
That last post was me.
Andybean9
Andybean9
My base object for objects that can be clicked is declared like this:

public class ClickableGameObject extends StaticGameObject implements MouseListener


Then in the constructor of the object I make the call to add the mouseListener. So each object should have their own.
Andybean9
Andybean9
Do you mean that I should have 1 interface for mouse listening and pass the event to all objects that need it instead of doing it the way I am now where each object has its own mouse listener?
Addictman
Addictman
No, I mean that since you add the mouse listeners to your renderCanvas, you have one shared version of a MouseListener list. So until you call renderCanvas.removeMouseListener( object ), object will always have a reference, and thus not go away.

If you in each ClickableObject called this.addMouseListener(this) etc., each object would have its own. (Not saying this is a good idea at all, just for explanation).

Development blog, The Omega Sector -- http://www.omegasector.org
Andybean9
Andybean9
Alright, well then I basically know what I need to do, I'll make sure that I remove the instance from the mouseListener list in the Canvas. Is this a good way to be doing this or is there a better way? Thanks for the help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.