I just realized a rather interesting memoryleak in my program.
Input (keyboard, mouse) is centralized by a KeyTracker and a MouseTracker, both of which exists only one. The input are wired into these Trackers as soon as the window is created.
When a certain object (a listener) wants to receive direct input, It registers itself at the Tracker. Whenever there is input, the Tracker will tell its listeners.
The camera is able to switch between two stances, and this switching is done by calling the camera-object that implements the desired stance. One camera-object is for instance controlled by the mouse (and there is where the problem starts).
In Java, destroying an object is done by forgetting about it, you basically replace it and the garbage collector will eventually clean it up. However, the Tracker will always keep a reference to each of its listeners, and thus the camera is never forgotten/cleaned. Result: switching the camera often will pile up obsolete camera’s in memory.
A solution would be to let registering and unregistering be dealt with by the caller of the cameras (and all other listeners) -> this adds complexity. Other option is using a “weak reference” in the Tracker, but i doubt that is good practice.
Other option, the one I aim at, is making a Listener manager, that restricts the usage of every listener, such that a memory leak is no longer possible.