mardi 22 janvier 2019

If I use std::function to capture a lambda, should I worry about it being deallocated?

Consider the following code,

std::function<void(void)> f = []() {};
std::function<void(void)>* fp = new std::function<void(void)>(f);

Whenever the program leaves the context of these lines (let's say they were called inside a function and the function returns) f and fp's memory may be reclaimed at some point. Which is fine because I'm only interested in fp and I got a copy of it.

The point of this is having a function pointer (fp) that I can later invoke whenever I want. So, as long as I don't delete fp, the question is:

Will f ever be deallocated, rendering fp invalid?

Aucun commentaire:

Enregistrer un commentaire