变量在调用 lambda 之前被销毁
Variable getting destroyed before calling lambda
我正在尝试构建一个用一些 pre/post 操作包装一些输入函数的 lambda。
- 我的代码工作正常,如果我尝试包装常规 function/lambda,pre/post 操作会被正确调用。
- 但是,当我尝试将我的装饰 lambda 应用于它之前生成的函数时,我的程序在抱怨内部函数在某个时候被释放后崩溃了(这已被 valgrind 确认)。
令我困惑的是,崩溃取决于编译器:代码在 Xcode 6 clang
(基于 clang-3.6
)上运行良好,但在 linux 上使用 [=14= 时崩溃] 和 g++4.8.4
.
我制作了一个重现该行为的小程序:
#include <iostream>
#include <string>
#include <functional>
using namespace std;
typedef function<void(void)> NestedFn;
int main()
{
// Create a cfunction
auto lambdaFactory = [&](string title, NestedFn nestedFunc)
{
// title is copied to the new lambda
return [&, title]() {
cerr << "------------ START -----------" << endl;
cerr << "Inside: " << title << endl;
nestedFunc();
cerr << "------------- END ------------" << endl;
};
}
auto l1 = lambdaFactory("1", []() { cerr << "\tNest (1)" << endl; });
auto l2 = lambdaFactory("2", []() { cerr << "\tNest (2)" << endl; });
l1(); // Works ok, displays, START, 1, END
l2(); // Same here
auto dobble = lambdaFactory("Dobble", l1);
dobble(); // Display START, Inside Dobble, START,
// then crashes when trying to execute nestedFunc(), ie l1()
}
我在变量作用域管理中做错了什么?这个程序有什么原因 not 使用 Apple 的 LLVM 崩溃吗?
编辑
作为记录,这是 T.C. 建议更正后的正确 lambdaFactory
:
auto lambdaFactory = [&](string title, NestedFn nestedFunc)
{
return [&, title, nestedFunc]() {
cerr << "------------ START -----------" << endl;
cerr << "Inside: " << title << endl;
nestedFunc();
cerr << "------------- END ------------" << endl;
};
};
调用 lambdaFactory
返回的 lambda 通过引用捕获 nestedFunc
,但是 nestedFunc
是按值传递的函数参数,因此一旦调用 lambdaFactory
returns,导致悬空引用。
And is there any reason for this program not crashing using Apple's LLVM ?
未定义行为未定义。您也可能使用两种不同的标准库实现(Mac/libstdc++ linux 上的 libc++),因此在布局等方面可能存在差异
我正在尝试构建一个用一些 pre/post 操作包装一些输入函数的 lambda。
- 我的代码工作正常,如果我尝试包装常规 function/lambda,pre/post 操作会被正确调用。
- 但是,当我尝试将我的装饰 lambda 应用于它之前生成的函数时,我的程序在抱怨内部函数在某个时候被释放后崩溃了(这已被 valgrind 确认)。
令我困惑的是,崩溃取决于编译器:代码在 Xcode 6 clang
(基于 clang-3.6
)上运行良好,但在 linux 上使用 [=14= 时崩溃] 和 g++4.8.4
.
我制作了一个重现该行为的小程序:
#include <iostream>
#include <string>
#include <functional>
using namespace std;
typedef function<void(void)> NestedFn;
int main()
{
// Create a cfunction
auto lambdaFactory = [&](string title, NestedFn nestedFunc)
{
// title is copied to the new lambda
return [&, title]() {
cerr << "------------ START -----------" << endl;
cerr << "Inside: " << title << endl;
nestedFunc();
cerr << "------------- END ------------" << endl;
};
}
auto l1 = lambdaFactory("1", []() { cerr << "\tNest (1)" << endl; });
auto l2 = lambdaFactory("2", []() { cerr << "\tNest (2)" << endl; });
l1(); // Works ok, displays, START, 1, END
l2(); // Same here
auto dobble = lambdaFactory("Dobble", l1);
dobble(); // Display START, Inside Dobble, START,
// then crashes when trying to execute nestedFunc(), ie l1()
}
我在变量作用域管理中做错了什么?这个程序有什么原因 not 使用 Apple 的 LLVM 崩溃吗?
编辑
作为记录,这是 T.C. 建议更正后的正确 lambdaFactory
:
auto lambdaFactory = [&](string title, NestedFn nestedFunc)
{
return [&, title, nestedFunc]() {
cerr << "------------ START -----------" << endl;
cerr << "Inside: " << title << endl;
nestedFunc();
cerr << "------------- END ------------" << endl;
};
};
调用 lambdaFactory
返回的 lambda 通过引用捕获 nestedFunc
,但是 nestedFunc
是按值传递的函数参数,因此一旦调用 lambdaFactory
returns,导致悬空引用。
And is there any reason for this program not crashing using Apple's LLVM ?
未定义行为未定义。您也可能使用两种不同的标准库实现(Mac/libstdc++ linux 上的 libc++),因此在布局等方面可能存在差异