Double free in the C++ standard library using only std::function and std::shared_pointer

I believe the relevant part of the standard is [res.on.objects], which states

If an object of a standard library type is accessed, and the beginning
of the object’s lifetime does not happen before the access, or the
access does not happen before the end of the object’s lifetime, the
behavior is undefined unless otherwise specified.

In your example, you access the std::function by assigning to it. During this access, the destructor of the std::function is called, ending the lifetime of the std::function. But the access has not finished, so it is not the case that the access happens before the end of the object’s lifetime.

Therefore, the code has undefined behavior.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)