Skip to content

Commit 9305ca8

Browse files
committed
Add missing std::forward calls for perfect forwarding
If the shared_ptr is copied, the final release may end up on a different thread. Adding std::forward will ensure that the lambda is moved and not copied which will prevent multiple threads from owning the shared_ptr. The shared_ptr is being used as a hack anyways. The correct fix is to not use shared_ptr and make Dispatch use the Dispatchable class from AppRuntime or if we can use C++23 someday, use std::move_only_function.
1 parent b4a3c73 commit 9305ca8

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

Core/JsRuntime/Include/Babylon/JsRuntimeScheduler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace Babylon
108108
template<typename CallableT>
109109
void operator()(CallableT&& callable) const
110110
{
111-
m_parent.Dispatch(callable);
111+
m_parent.Dispatch(std::forward<CallableT>(callable));
112112
}
113113

114114
private:
@@ -129,7 +129,7 @@ namespace Babylon
129129
}
130130
else
131131
{
132-
m_disposingDispatcher(callable);
132+
m_disposingDispatcher(std::forward<CallableT>(callable));
133133
}
134134

135135
--m_count;

Polyfills/Window/Source/TimeoutDispatcher.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ namespace Babylon::Polyfills::Internal
5353

5454
if (time <= earliestTime)
5555
{
56-
m_runtimeScheduler.Get()([this]()
57-
{
56+
m_runtimeScheduler.Get()([this]() {
5857
m_condVariable.notify_one();
5958
});
6059
}
@@ -142,8 +141,7 @@ namespace Babylon::Polyfills::Internal
142141
{
143142
if (function)
144143
{
145-
m_runtimeScheduler.Get()([function = std::move(function)]()
146-
{
144+
m_runtimeScheduler.Get()([function = std::move(function)]() {
147145
function->Call({});
148146
});
149147
}

0 commit comments

Comments
 (0)