Below code fails with BAD_ACCESS when I call s_capture_void_int() in the last line and I do not understand why. I suppose that when I assign lambda expression to a global variable it supposed to copy itself together with captured values. So in my understanding dangling references should not appear. But it looks like I'm missing something.
std::function<void()> s_capture_void_int;
void capture_void_int (const std::function<void(int)>& param)
{
s_capture_void_int = [param]() {
param(1);
};
}
void capture_local_lambda()
{
auto local_lambda = [](int) {
};
s_capture_void_int = [local_lambda]() {
local_lambda(1);
};
}
BOOST_AUTO_TEST_CASE( test_lambda_captures )
{
//Case 1: this works
auto func2 = [](int){};
{
std::function<void(int)> func2_fn(func2);
s_capture_void_int = [func2_fn]() { func2_fn(1); };
}
s_capture_void_int();
//case 2: even this works.
capture_local_lambda();
s_capture_void_int();
//case 3: but this fails.
auto func3 = [](int){};
{
std::function<void(int)> func3_fn(func3);
capture_void_int(func3_fn);
}
s_capture_void_int(); //<- it crashes here
}
I don't understand two things here:
I'd say it's a compiler bug. It works fine in GCC. Perhaps the param
in capture_void_int
is incorrectly captured by reference (since it's a reference) when it should instead be captured by value.