JSON API avoid capturing shared_ptr in lambda

use weak_ptr as capture shared_ptr could cause circular ref counting
This commit is contained in:
Gioacchino Mazzurco 2019-04-15 00:07:21 +02:00
parent 954644ef57
commit f2aa2aa543
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
2 changed files with 43 additions and 35 deletions

View file

@ -1,20 +1,22 @@
/* /*******************************************************************************
* RetroShare JSON API * RetroShare JSON API *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> * *
* * Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* This program is free software: you can redistribute it and/or modify * *
* it under the terms of the GNU Affero General Public License as * This program is free software: you can redistribute it and/or modify *
* published by the Free Software Foundation, either version 3 of the * it under the terms of the GNU Affero General Public License as *
* License, or (at your option) any later version. * published by the Free Software Foundation, either version 3 of the *
* * License, or (at your option) any later version. *
* This program is distributed in the hope that it will be useful, * *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * This program is distributed in the hope that it will be useful, *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* GNU Affero General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* * GNU Affero General Public License for more details. *
* You should have received a copy of the GNU Affero General Public License * *
* along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU Affero General Public License *
*/ * along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
registerHandler("$%apiPath%$", registerHandler("$%apiPath%$",
[$%captureVars%$](const std::shared_ptr<rb::Session> session) [$%captureVars%$](const std::shared_ptr<rb::Session> session)
@ -41,8 +43,12 @@ $%paramsDeclaration%$
$%inputParamsDeserialization%$ $%inputParamsDeserialization%$
$%callbackName%$ = [session]($%callbackParams%$) const std::weak_ptr<rb::Session> weakSession(session);
$%callbackName%$ = [weakSession]($%callbackParams%$)
{ {
auto session = weakSession.lock();
if(!session || session->is_closed()) return;
$%callbackParamsSerialization%$ $%callbackParamsSerialization%$
std::stringstream message; std::stringstream message;

View file

@ -1,20 +1,22 @@
/* /*******************************************************************************
* RetroShare JSON API * RetroShare JSON API *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> * *
* * Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* This program is free software: you can redistribute it and/or modify * *
* it under the terms of the GNU Affero General Public License as * This program is free software: you can redistribute it and/or modify *
* published by the Free Software Foundation, either version 3 of the * it under the terms of the GNU Affero General Public License as *
* License, or (at your option) any later version. * published by the Free Software Foundation, either version 3 of the *
* * License, or (at your option) any later version. *
* This program is distributed in the hope that it will be useful, * *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * This program is distributed in the hope that it will be useful, *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* GNU Affero General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* * GNU Affero General Public License for more details. *
* You should have received a copy of the GNU Affero General Public License * *
* along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU Affero General Public License *
*/ * along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
registerHandler("$%apiPath%$", registerHandler("$%apiPath%$",
[$%captureVars%$](const std::shared_ptr<rb::Session> session) [$%captureVars%$](const std::shared_ptr<rb::Session> session)