MCPcopy Create free account
hub / github.com/Snapchat/Valdi / dispatch

Method dispatch

valdi/src/valdi/android/MainThreadDispatcher.cpp:24–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22MainThreadDispatcher::~MainThreadDispatcher() = default;
23
24void MainThreadDispatcher::dispatch(Valdi::DispatchFunction* function, bool sync) {
25 if (sync) {
26 std::promise<void> promise;
27 auto future = promise.get_future();
28
29 // Capture `function` by value (copy the pointer) to avoid a dangling reference.
30 // The original code used [&] which captured `function` by reference to the stack
31 // variable. After promise.set_value(), the calling thread wakes up from future.get()
32 // and could potentially destroy its stack frame before `delete function` executes,
33 // causing a use-after-free on the `function` pointer itself.
34 auto wrappedFn = new Valdi::DispatchFunction([function, &promise]() {
35 (*function)();
36 promise.set_value();
37 delete function;
38 });
39
40 _runOnMainThreadMethod.call(toObject(), reinterpret_cast<int64_t>(wrappedFn));
41 future.get();
42 } else {
43 _runOnMainThreadMethod.call(toObject(), reinterpret_cast<int64_t>(function));
44 }
45}
46
47void MainThreadDispatcher::dispatchAfter(int64_t delayMs, Valdi::DispatchFunction* function) {
48 _runOnMainThreadDelayedMethod.call(toObject(), delayMs, reinterpret_cast<int64_t>(function));

Callers 3

TESTMethod · 0.45
callSyncWithJsThreadMethod · 0.45
scheduleFlushMethod · 0.45

Calls 4

toObjectFunction · 0.85
set_valueMethod · 0.80
callMethod · 0.65
getMethod · 0.65

Tested by 1

TESTMethod · 0.36