Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use payload with static storage duration to avoid memory leak.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 2 Mar 2022 21:26:51 +0000 (22:26 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 2 Mar 2022 21:26:51 +0000 (22:26 +0100)
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.

teshsuite/s4u/host-on-off-actors/host-on-off-actors.cpp

index a9b7063..f394ea4 100644 (file)
@@ -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<std::string>();
+    auto payload = simgrid::s4u::Mailbox::by_name("comm")->get<std::string>();
     XBT_INFO("  Receive message: %s", payload->c_str());
   } catch (const simgrid::HostFailureException&) {
     XBT_INFO("  Receive message: HOST_FAILURE");