Code RoomLambda captures destroyed object
HardPrep Room Coding #1802

Lambda captures destroyed object

Code reviewCode quality & reviewSenior–Staff~30 min

Review this C++ async registration using a lambda.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 30 min
Mark a line and say what kind of problem it is.0 findings
1#include <functional>
2#include <string>
3 
4struct Worker {
5 std::string name;
6 void schedule(std::function<void()>& slot) {
7 slot = [this]() { use(name); };
8 }
9 void use(const std::string& s);
10};
11 
12std::function<void()> g_slot;
13void wire() {
14 std::function<void()> tmp;
15 { Worker w{"alpha"}; w.schedule(reinterpret_cast<std::function<void()>&>(tmp)); }
16 g_slot = tmp;
17 g_slot(); // invoked later
18}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.