#include <iostream>
#include <functional>
int global = 9;
std::function<void()> functor;
int main()
{
int* ptr = &global;
functor = [ptr]
{
functor = nullptr;
std::cout << *ptr << std::endl;
};
functor();
}
Here is variable ptr
captured by lambda, and during functor()
call functor first deleted through functor = nullptr
and then accesses ptr
. I think that ptr
was corrupted since it was a field of a deleted functor. All the compilers successfully performs that program without crashes and print "9", but I still doubt that this is not undefined behavior. Can someone confirm it?