From 1de8193cea11e0ca5d161976d3704014b9ed5aa9 Mon Sep 17 00:00:00 2001 From: marty1885 Date: Wed, 4 Oct 2023 22:21:41 +0800 Subject: [PATCH] fix corruption again --- gnunetpp/gnunetpp-messenger.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/gnunetpp/gnunetpp-messenger.cpp b/gnunetpp/gnunetpp-messenger.cpp index 9f2777d..68e7a91 100644 --- a/gnunetpp/gnunetpp-messenger.cpp +++ b/gnunetpp/gnunetpp-messenger.cpp @@ -184,15 +184,17 @@ GNUNET_HashCode Room::getId() const static std::pair message_convert(const UserSendibleMessage &value) { GNUNET_MESSENGER_Message msg; - std::string dup; if(value.index() == std::variant_npos) return {msg, std::any()}; + + std::shared_ptr holder = nullptr; + switch(value.index()) { case 0: msg.header.kind = GNUNET_MESSENGER_KIND_TEXT; // HACK: We need to keep the string alive until the message is sent - dup = std::get<0>(value).text; - msg.body.text.text = (char*)dup.c_str(); + holder = std::make_shared(std::get<0>(value).text); + msg.body.text.text = (char*)holder->c_str(); break; case 1: // Don't know how to send files yet @@ -200,8 +202,8 @@ static std::pair message_convert(const UserS break; case 2: msg.header.kind = GNUNET_MESSENGER_KIND_NAME; - dup = std::get<2>(value).name; - msg.body.name.name = (char*)dup.c_str(); + holder = std::make_shared(std::get<2>(value).name); + msg.body.name.name = (char*)holder->c_str(); break; case 3: msg.header.kind = GNUNET_MESSENGER_KIND_INVITE; @@ -212,7 +214,9 @@ static std::pair message_convert(const UserS // Should never happen throw std::runtime_error("Unknown message type"); } - return {msg, std::move(dup)}; + + std::any res = std::move(holder); + return {msg, res}; } void Room::sendMessage(const std::string &value)