Adding "writeback" callbacks to the depsgraph should be temporary, since these callbacks can bind pointers that become invalid. In this case: the evaluated nodes modifier bound by a bake node callback. These callbacks are cleared after being added in the `deg_flush_updates_and_refresh` function, but there are other cases where depsgraph updates are executed which don't support writeback callbacks (`object_force_modifier_update_for_bind` run by the smooth modifier "bind" function). To prevent dangling invalid pointers in outdated callbacks, disallow adding callbacks in any case other than the `deg_flush_updates_and_refresh` function. Since callbacks can be added from any depsgraph update, the safest way to prevent adding them is in the depsgraph itself. If the `use_writeback_callbacks` flag is not set, any callback is simply discarded. Pull Request: https://projects.blender.org/blender/blender/pulls/137083
26 lines
678 B
C++
26 lines
678 B
C++
/* SPDX-FileCopyrightText: 2024 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include <mutex>
|
|
|
|
#include "DEG_depsgraph.hh"
|
|
#include "DEG_depsgraph_writeback_sync.hh"
|
|
|
|
#include "depsgraph.hh"
|
|
|
|
namespace blender::deg::sync_writeback {
|
|
|
|
void add(::Depsgraph &depsgraph, std::function<void()> fn)
|
|
{
|
|
deg::Depsgraph °_graph = reinterpret_cast<deg::Depsgraph &>(depsgraph);
|
|
if (!deg_graph.is_active || deg_graph.sync_writeback == DEG_EVALUATE_SYNC_WRITEBACK_NO) {
|
|
return;
|
|
}
|
|
|
|
std::lock_guard lock{deg_graph.sync_writeback_callbacks_mutex};
|
|
deg_graph.sync_writeback_callbacks.append(std::move(fn));
|
|
}
|
|
|
|
} // namespace blender::deg::sync_writeback
|