<Swing Dev> Is the following code supposed to leak?
Pavel Porvatov
pavel.porvatov at oracle.com
Mon Aug 22 13:04:01 UTC 2011
Hi Clemens,
> Hi Roman,
>
> You keep creating and adding new listeners without ever removing them.
> Of course this is going to leak.
>
>
> What makes me wonder, I add the listeners to an Action from a newly
> created JTextArea.
> Shouldn't the action belong to the JTextArea itself and be
> garbage-collected too when the JTextArea is no longer reachable? (like
> its the case for Listeners attached to the Component directly like
> Mouse/Key/...-Listeners)
> In the case of Notepad, that would mean Swing would require the
> application to remove the listeners manually?
In common case you shouldn't remove listeners of course. But in your
case you must remove listeners because of editor is shared between all
JTextComponents (see javadoc of the JTextComponent#getActions method and
BasicTextUI#getEditorKit implementation). So you added listeners not to
a JTextComponent instance, but to the shared editor.
>
> I have to admit I don't understand the concept of Actions in detail...
>
> It's not even related to the fact that
> the listener references the array (although that could play a role in
> more complicated leaking scenarios)
>
> Sure, the array is just there to trigger the OOM faster ;).
>
>
> Thanks, Clemens
Regards, Pavel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20110822/22155d59/attachment.html>
More information about the swing-dev
mailing list