From: Arnaud Giersch Date: Wed, 2 Mar 2022 21:26:51 +0000 (+0100) Subject: Use payload with static storage duration to avoid memory leak. X-Git-Tag: v3.31~254 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/b0d49454a48ab619e18ba8c638c3ceda8e1d882e Use payload with static storage duration to avoid memory leak. Since comm is detached, the sender has no way to know when it's received, and correctly release or delete the payload in any case. --- diff --git a/teshsuite/s4u/host-on-off-actors/host-on-off-actors.cpp b/teshsuite/s4u/host-on-off-actors/host-on-off-actors.cpp index a9b70634b2..f394ea42d9 100644 --- a/teshsuite/s4u/host-on-off-actors/host-on-off-actors.cpp +++ b/teshsuite/s4u/host-on-off-actors/host-on-off-actors.cpp @@ -25,15 +25,14 @@ XBT_ATTRIB_NORETURN static void actor_daemon() static void commTX() { XBT_INFO(" Start TX"); - auto* payload = new std::string("COMM"); - simgrid::s4u::Mailbox::by_name("comm")->put_init(payload, 100000000)->detach(); + static std::string payload = "COMM"; + simgrid::s4u::Mailbox::by_name("comm")->put_init(&payload, 100000000)->detach(); // We should wait a bit (if not the process will end before the communication, hence an exception on the other side). try { simgrid::s4u::this_actor::sleep_for(30); } catch (const simgrid::HostFailureException&) { XBT_INFO("The host has died ... as expected."); } - delete payload; XBT_INFO(" TX done"); } @@ -42,7 +41,7 @@ static void commRX() { XBT_INFO(" Start RX"); try { - auto payload = simgrid::s4u::Mailbox::by_name("comm")->get_unique(); + auto payload = simgrid::s4u::Mailbox::by_name("comm")->get(); XBT_INFO(" Receive message: %s", payload->c_str()); } catch (const simgrid::HostFailureException&) { XBT_INFO(" Receive message: HOST_FAILURE");