Skip to content

Commit

Permalink
8290331: Binding value left null when immediately revalidated in inva…
Browse files Browse the repository at this point in the history
…lidation listener

Reviewed-by: mstrauss, kcr
  • Loading branch information
hjohn authored and kevinrushforth committed Jul 15, 2022
1 parent b8302f6 commit 4959f1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ public final void invalidate() {
valid = false;
onInvalidating();
ExpressionHelper.fireValueChangedEvent(helper);
value = null; // clear cached value to avoid hard reference to stale data

/*
* Cached value should be cleared to avoid a strong reference to stale data,
* but only if this binding didn't become valid after firing the event:
*/

if (!valid) {
value = null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ void shouldBeInvalidInitially() {
assertFalse(binding.isValid());
}

@Test
void invalidationWhichBecomesValidDuringCallbacksShouldReturnCorrectValue() {
LazyObjectBindingStub<String> binding = new LazyObjectBindingStub<>() {
@Override
protected String computeValue() {
return "A";
}
};

binding.addListener(obs -> {
assertEquals("A", binding.get());
});

binding.invalidate(); // becomes valid again immediately

assertEquals("A", binding.get());
}

@Nested
class WhenObservedWithInvalidationListener {
private InvalidationListener invalidationListener = obs -> {};
Expand Down

1 comment on commit 4959f1b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.