RFR: 8274771: Map, FlatMap and OrElse fluent bindings for ObservableValue [v18]

John Hendrikx john.hendrikx at gmail.com
Wed Jul 6 13:52:04 UTC 2022


I posted a reply on Github, but it's not making it to the list (that I 
can see) for some reason.

--John

On 05/07/2022 19:18, Michael Strauß wrote:
> On Fri, 1 Jul 2022 15:16:24 GMT, John Hendrikx <jhendrikx at openjdk.org> wrote:
>
> I have yet another question. The following test passes for `Bindings.select`, but fails for `ObservableValue.flatMap`:
>
>
> JMemoryBuddy.memoryTest(test -> {
>      class ValueHolder {
>          final StringProperty value = new SimpleStringProperty(this, "value");
>          StringProperty valueProperty() { return value; }
>      }
>
>      ObjectProperty<ValueHolder> valueHolderProperty = new SimpleObjectProperty<>();
>      valueHolderProperty.set(new ValueHolder());
>
>      // Map the nested property value
>      ObservableValue<String> mapped = valueHolderProperty.flatMap(ValueHolder::valueProperty);
>      
>      // Note: the test passes when using the following alternative to flatMap:
>      // ObservableValue<String> mapped = Bindings.selectString(valueHolderProperty, "value");
>
>      // Bind the mapped value to a property that will soon be GC'ed.
>      ObjectProperty<String> otherProperty = new SimpleObjectProperty<>();
>      otherProperty.bind(mapped);
>
>      test.setAsReferenced(valueHolderProperty);
>      test.assertCollectable(otherProperty);
>      test.assertCollectable(mapped); // expectation: the mapped value is eligible for GC
> });
>
>
> My observation is that a flat-mapped value that was once observed is not eligible for garbage-collection even when the observer itself is collected. This seems to be quite unexpected to me, because it means that a bound property that is collected without being manually unbound will cause a memory leak in the mapped binding.
>
> Is this by design? If so, I think this can lead to subtle and hard to diagnose bugs, and should be documented at the very least.
>
> -------------
>
> PR: https://git.openjdk.org/jfx/pull/675


More information about the openjfx-dev mailing list