Skip to content

Commit

Permalink
Slider: Avoid updating internal Value when old and new values are bot…
Browse files Browse the repository at this point in the history
…h NaN

Without this change in place, setting the Value to NaN can cause a stack
overflow because the old and new values always compare unequal, causing
new change notifications to be sent.
  • Loading branch information
reuk committed Jun 11, 2024
1 parent 2a26439 commit 182dd84
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion modules/juce_gui_basics/widgets/juce_Slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ class Slider::Pimpl : public AsyncUpdater, // this needs to be public otherwis
// Need to do this comparison because the Value will use equalsWithSameType to compare
// the new and old values, so will generate unwanted change events if the type changes.
// Cast to double before comparing, to prevent comparing as another type (e.g. String).
if (! approximatelyEqual (static_cast<double> (currentValue.getValue()), newValue))
// We also want to avoid sending a notification if both new and old values are NaN.
const auto asDouble = static_cast<double> (currentValue.getValue());

if (! (approximatelyEqual (asDouble, newValue) || (std::isnan (asDouble) && std::isnan (newValue))))
currentValue = newValue;

updateText();
Expand Down

0 comments on commit 182dd84

Please sign in to comment.