RFR: 8290040: Provide simplified deterministic way to manage listeners [v6]

Andy Goryachev angorya at openjdk.org
Thu Oct 13 22:19:10 UTC 2022


On Thu, 13 Oct 2022 22:11:12 GMT, Andy Goryachev <angorya at openjdk.org> wrote:

>> Thank you for explanations, @hjohn .
>> 
>> This *might* work for Skins, of course, though it is not exactly what is needed for Skins (I will explain why in https://github.com/openjdk/jfx/pull/908 because of context).
>> 
>> In the context of this PR, I can see how a memory leak can be created when a developer has the `active` property as long lived and that would prevent the whole chain from being collected.  An example would be trying to attach to the Window.showingProperty(), adding listeners that reference Nodes that are supposed to be GC'd when removed from the said window (dynamically creating detail panes as response to selection event, for example).
>> 
>> I think what you propose here is good, but, as any other place where listeners are added, must be used with caution (may be reflect that fact in javadoc?)
>
> And, more specifically, is there a way to break the strong reference between the `active` property and the dependent fluent chain?

Also, using your example above, it might be too easy to create a memory leak by inadvertently referring to a long lived object:


getSkinnable().textProperty().when(active).addListener((src,prev,current) -> {
  getSkinnable().setSomething(window.getSomething() + "..."); // window reference makes the listener lambda uncollectable, resulting in a memory leak
});

-------------

PR: https://git.openjdk.org/jfx/pull/830


More information about the openjfx-dev mailing list