"Back referencing"

Hello everyone,

SHORT VERSION FIRST, detailed explanation follows:

I need to go through all of my existing objects and "null" all references to a certain given object so that that object would be garbage collected... I'm looking for a short cut way of doing this thorugh the JVM maybe?

Full Explanation:

I recently "switched" from C++ to Java for game programming and I must admit, I was emazed by the capability of the powerful Java libraries.

I have a small question.

In my game, objects interact in various ways that make them keep references to their counterparts... a missile might have a vessel object as a target, another vessel may have a reference to the same object as that missile's target and so forth...

My trouble is, that when that supposed missile hits and destroys its target, I need to remove the target object, but I can't! There are just too many objects to go through and "disconnect" this destroyed object from.

Of course that it is possible to do, no question about that... going through all objects in the game and turning all references to that object to "null" is not a real problem, but it can get awefully complicated...

Because java emplys a virtual machine, perhaps it could be possible for me to specify the object reference and tell the virtual machine to "null" all existing references to that object?

Thank you for your time.

[1429 byte] By [Warlaxa] at [2007-11-26 17:14:30]
# 1

> SHORT VERSION FIRST, detailed explanation follows:

> I need to go through all of my existing objects and

> "null" all references to a certain given object so

> that that object would be garbage collected... I'm

> looking for a short cut way of doing this thorugh the

> JVM maybe?

You have an expert working on your side doing this all the time called the garbage collector. It's automatic and you don't have to do anything. If you have put yourself in a position where you're considering duplicating its job you have a bad OO design.

> I have a small question.

> In my game, objects interact in various ways that

> make them keep references to their counterparts... a

> missile might have a vessel object as a target,

> another vessel may have a reference to the same

> object as that missile's target and so forth...

> My trouble is, that when that supposed missile hits

> and destroys its target, I need to remove the target

> object, but I can't! There are just too many objects

> to go through and "disconnect" this destroyed object

> from.

What you seem to have is object "spagetti". Try to reorganize your program so objects are more detached.

One way that could work in your case is the so called Mediator pattern. Listener objects attach themselves to a common Mediator object thereby subscribing to the messages it broadcasts. When an object gets "killed" it tells the Mediator of its unfortunate demise and the Mediator in its turn broadcasts this to all its Listeners. Each Listener object then take appropriate action, like for example releasing its reference to the "dead" object. When this is done (and the object no longer is referenced from anywhere in the program) the GC automatically will remove it.

Warlaxa at 2007-7-8 23:42:28 > top of Java-index,Java Essentials,Java Programming...
# 2

The normal way would be for you to provide an interface

interface DestroyedListener

{

public void targetDestroyed(DestroyedEvent event);

}

The vessel would provide two methods

addDestroyedListener(DestroyedListener listener)

removeDestroyedListener(DestroyedListener listener)

When one vessel targets another, the first vessel would do this;

target.addDestroyedListener(this);

The vessel that is called then would just add this listener to a set of listeners.

When the vessel is destroyed, it loops over all items in the set and call

DestroyedEvent event = new DestroyedEvent(this); // i was destroyed

for (DestroyedListener listener : listeners)

listener.targetDestroyed(event);

when the listener receives the method call, it can clear any references it holds

dmbdmba at 2007-7-8 23:42:28 > top of Java-index,Java Essentials,Java Programming...
# 3

Another way is to use indirect references rather than direct object references.

In this scenario each vechicle is represented by a unique id number rather than its reference. To connect the id with with the vehicle object you use a HashMap. When a vehicle "dies" it just removes its HashMap entry and it's gone. Other objects will later detect this when they realize there's no entry left in the HashMap BUT in the meantime they won't hold on to an object reference so the GC isn't prevented from doing its work.

In both cases, the Mediator pattern and the indirect references, you accomplish a higher detachment between object. The best design strategy often is to use several of these techniques.

dmbdmba at 2007-7-8 23:42:28 > top of Java-index,Java Essentials,Java Programming...
# 4
I take it he fled back to C++ again where you can program in C thinking you're using OO.
dmbdmba at 2007-7-8 23:42:28 > top of Java-index,Java Essentials,Java Programming...
# 5
well, using the jdi would have been alot move l337
dmbdmba at 2007-7-8 23:42:28 > top of Java-index,Java Essentials,Java Programming...