WeakReferences

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

WeakReferences

frigoref
I thought about cleaning up some stuff in addition to a patch I submitted concerning the UI panels.
While checking the code I found something that surprised me:
(games.strategy.triplea.ui.AbstractMovePanel)

    protected AbstractAction m_doneMove = new AbstractAction("Done")
    {
        public void actionPerformed(ActionEvent e)
        {
            if (doneMoveAction())
            {
                m_moveMessage = null;
                release();
            }
        }
    };
    private final Action m_DONE_MOVE_ACTION = new WeakAction("Done", m_doneMove);

m_doneMove is never used again and m_DONE_MOVE_ACTION is only used as a parameter for a constructor of a JButton.
Following the reference to WeakAction brings:
(games.strategy.triplea.ui.MovePanel)

class WeakAction extends AbstractAction
{
    private final WeakReference<Action> m_delegate;
   
    WeakAction(String name, Action delegate)
    {
        super(name);
        m_delegate = new WeakReference<Action>(delegate);
    }
   
    public void actionPerformed(ActionEvent e)
    {
        Action a = m_delegate.get();
        if (a != null)
            a.actionPerformed(e);
    }
}

When I understood weak references correctly, the first part tries to give m_doneMove a chance to be deleted assuming it wouldn't be automatically when the AbstractMovePanel is deleted.

I think what the author of this code wants to do is this: As the parameter of the JButton (code not posted) is an Action object (m_DONE_MOVE_ACTION) it will be listed as ActionListener for the button and will stay even if the Action is deleted in AbstractMovePanel - that's the problem of SWING. Therefore a weak reference should be employed to add the ActionListener to the JButton and remove it if the Action is not reachable via a strong reference (and the garbage collector has removed it) - like in http://bytes.com/topic/java/insights/705335-weakreferences.

The way it is done now seems very strange to me as strong references of m_DONE_MOVE_ACTION and m_doneMove remain in the object AbstractMovePanel until it is deleted. I just don't see why both are needed.
Do I make a mistake by thinking the weak reference is used in the wrong way? Wouldn't it be enough to construct the JButton without any parameter and then use an ActionReference class (like in the link shown above) to add m_doneMove as a listener?
Or is it even simpler and I do not need any weak references as when the panel gets, then the JButton gets deleted along with every ActionListener involved?

I would appreciate your help as I am a bit confused and I think I could learn something if this problem gets solved.
Reply | Threaded
Open this post in threaded view
|

Re: WeakReferences

Veqryn
Administrator
i know nothing about java ui,
and i have no clue what you are talking about,
my only comment is that everything appears to be working without any bugs, so i'm not too excited to mess with it unless someone truly understands what they are doing

which is why i hope SGB or comradekev can comment on this thread


thx,
veq
Please contribute to the TripleA 2013 donation drive:
http://tripleadev.1671093.n2.nabble.com/2013-TripleA-Donation-Drive-tp7583455.html
Reply | Threaded
Open this post in threaded view
|

Re: WeakReferences

frigoref
This post was updated on .
Sure thing, I was just trying to understand that piece of code.
I won't change anything that I do not understand. You can see that at the comments I made for every change.

I would really appreciate if SGB or comradekev could explain what this code is doing/why it is necessary.
Reply | Threaded
Open this post in threaded view
|

Re: WeakReferences

frigoref
@SGB or comradekev: *PUSH*
Reply | Threaded
Open this post in threaded view
|

Re: WeakReferences

Sean Bridges
Administrator
There was a bug where JFrame's were not being garbage collected.  I wanted to break the link from JFrame -> GameData.  One of those links was the JFrame->Panel->Button->ActionListener->GameData.  

There are some comments in TripleAFrame,

        //there is a bug in java (1.50._06  for linux at least)
        //where frames are not garbage collected.
        //
        //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6364875
        //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6368950
        //
        //so remove all references to everything
        //to minimize the damage

And a lot more work done there to break the links.  The code in TripleAFrame if probably breaking the link from JFrame->Panel, so this further link breaking with Weak references may not be needed, but it was a long time ago, so further testing should be done. To test, run with a profiler, starting and stopping games, and see how many GameData objects there are.