From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp57.i.mail.ru (smtp57.i.mail.ru [217.69.128.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id BECD3445320 for ; Wed, 8 Jul 2020 15:15:35 +0300 (MSK) From: "Timur Safin" References: <1594210027-332-1-git-send-email-alyapunov@tarantool.org> <1594210027-332-2-git-send-email-alyapunov@tarantool.org> In-Reply-To: <1594210027-332-2-git-send-email-alyapunov@tarantool.org> Date: Wed, 8 Jul 2020 15:15:34 +0300 Message-ID: <027001d65521$7ba71ec0$72f55c40$@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Content-Language: ru Subject: Re: [Tarantool-patches] [PATCH] Fix wrong make_scoped_guard usage List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: 'Aleksandr Lyapunov' , tarantool-patches@dev.tarantool.org Explicit capture list in lambda - is the correct way, making things clearer! LGTM Timur : -----Original Message----- : From: Tarantool-patches On : Behalf Of Aleksandr Lyapunov : Sent: Wednesday, July 8, 2020 3:07 PM : To: tarantool-patches@dev.tarantool.org : Subject: [Tarantool-patches] [PATCH] Fix wrong make_scoped_guard usage : : 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. : --- : src/box/alter.cc | 4 ++-- : src/main.cc | 2 +- : 2 files changed, 3 insertions(+), 3 deletions(-) : : diff --git a/src/box/alter.cc b/src/box/alter.cc : index bb42548..3109fb4 100644 : --- a/src/box/alter.cc : +++ b/src/box/alter.cc : @@ -3560,7 +3560,7 @@ on_replace_dd_func(struct trigger * /* trigger */, : void *event) : * definition to support upgrade script. : */ : struct func_def *old_def = NULL, *new_def = NULL; : - auto guard = make_scoped_guard([=] { : + auto guard = make_scoped_guard([&old_def, &new_def] { : free(old_def); : free(new_def); : }); : @@ -4431,7 +4431,7 @@ on_replace_dd_sequence(struct trigger * /* trigger : */, void *event) : struct tuple *new_tuple = stmt->new_tuple; : : struct sequence_def *new_def = NULL; : - auto def_guard = make_scoped_guard([=] { free(new_def); }); : + auto def_guard = make_scoped_guard([&new_def] { free(new_def); }); : : struct sequence *seq; : if (old_tuple == NULL && new_tuple != NULL) { /* INSERT */ : diff --git a/src/main.cc b/src/main.cc : index 2c96391..e622042 100644 : --- a/src/main.cc : +++ b/src/main.cc : @@ -729,7 +729,7 @@ main(int argc, char **argv) : /* Lua interpeter options, e.g. -e and -l */ : int optc = 0; : const char **optv = NULL; : - auto guard = make_scoped_guard([=]{ if (optc) free(optv); }); : + auto guard = make_scoped_guard([&optc, &optv]{ if (optc) free(optv); : }); : : static struct option longopts[] = { : {"help", no_argument, 0, 'h'}, : -- : 2.7.4