I'm coding a network function on C++, the HTTP request in background thread, use lambda callback when receive HTTP data. But I don't know how to release the lambda, hopes some help.
void foo()
{
// the `func` must be a heap variable for asynchronously.
auto func = new auto ([&](std::string response){
printf("recv data: %s", response.c_str());
});
std::thread t([&]{
sleep(2); // simulate a HTTP request.
std::string ret = "http result";
(*func)(ret);
});
t.detach();
// The foo function was finished. bug `func` lambda still in memory ?
}
int main()
{
foo();
getchar(); // simulate UI Event Loop.
return 0;
}