[Tarantool-patches] [PATCH] Fix wrong make_scoped_guard usage

Cyrill Gorcunov gorcunov at gmail.com
Wed Jul 8 15:16:34 MSK 2020


On Wed, Jul 08, 2020 at 03:07:07PM +0300, Aleksandr Lyapunov wrote:
> The common pitfall of using a lambda is wrong type of capture -
> by value instead of by reference. The simple example is:
>   struct sequence_def *new_def = NULL;
>   auto def_guard = make_scoped_guard([=] { free(new_def); });
>   // initialize new_def
> The problem is that the lambda captures pointer by value, that
> is NULL and will remain NULL in the lambda while new_def is
> successfully initialized in function scope.
> 
> The patch fixes the problem above and a couple of similar mistakes.

I think we could use shorter [&] form but this is just
a personal preference. Thank you, good catch!

Reviewed-by: Cyrill Gorcunov <gorcunov at gmail.com>


More information about the Tarantool-patches mailing list