diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 2c8239513..c338932c6 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -3740,6 +3740,7 @@ void Simulation::BeforeSim() air->update_airh(); DispatchNewtonianGravity(); + // gravIn is now potentially garbage, which is ok, we were going to clear it for the frame anyway for (auto p : gravIn.mass.Size().OriginRect()) { gravIn.mass[p] = 0.f; @@ -3964,6 +3965,8 @@ void Simulation::ResetNewtonianGravity(GravityInput newGravIn, GravityOutput new { gravIn = newGravIn; DispatchNewtonianGravity(); + // gravIn is now potentially garbage, set it again + gravIn = newGravIn; if (grav) { gravOut = newGravOut; @@ -3981,7 +3984,10 @@ void Simulation::EnableNewtonianGravity(bool enable) if (!grav && enable) { grav = Gravity::Create(); + auto oldGravIn = gravIn; DispatchNewtonianGravity(); + // gravIn is now potentially garbage, set it again + gravIn = std::move(oldGravIn); } } diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index e4ea5ae44..94dc66608 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -220,10 +220,11 @@ public: ~Simulation(); void EnableNewtonianGravity(bool enable); - void ResetNewtonianGravity(GravityInput newGravIn, GravityOutput newGravOut); - void DispatchNewtonianGravity(); - void UpdateGravityMask(); private: CoordStack& getCoordStackSingleton(); + + void ResetNewtonianGravity(GravityInput newGravIn, GravityOutput newGravOut); + void DispatchNewtonianGravity(); + void UpdateGravityMask(); }; diff --git a/src/simulation/gravity/Fft.cpp b/src/simulation/gravity/Fft.cpp index 43757263f..5cec0295a 100644 --- a/src/simulation/gravity/Fft.cpp +++ b/src/simulation/gravity/Fft.cpp @@ -204,7 +204,7 @@ void GravityImpl::Init() }); } -void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn) +void Gravity::Exchange(GravityOutput &gravOut, GravityInput &gravIn) { auto *fftGravity = static_cast(this); @@ -231,7 +231,7 @@ void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn) std::memcmp(&fftGravity->gravIn.mask[{ 0, 0 }], &gravIn.mask[{ 0, 0 }], NCELL * sizeof(float))) { fftGravity->copyGravOut = true; - fftGravity->gravIn = gravIn; + std::swap(gravIn, fftGravity->gravIn); } fftGravity->Dispatch(); diff --git a/src/simulation/gravity/Gravity.h b/src/simulation/gravity/Gravity.h index cab143200..da14c0f5a 100644 --- a/src/simulation/gravity/Gravity.h +++ b/src/simulation/gravity/Gravity.h @@ -8,7 +8,8 @@ protected: Gravity() = default; public: - void Exchange(GravityOutput &gravOut, const GravityInput &gravIn); + // potentially clobbers gravIn + void Exchange(GravityOutput &gravOut, GravityInput &gravIn); static GravityPtr Create(); }; diff --git a/src/simulation/gravity/Null.cpp b/src/simulation/gravity/Null.cpp index c57b8d057..772f02560 100644 --- a/src/simulation/gravity/Null.cpp +++ b/src/simulation/gravity/Null.cpp @@ -1,6 +1,6 @@ #include "Gravity.h" -void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn) +void Gravity::Exchange(GravityOutput &gravOut, GravityInput &gravIn) { }