diff --git a/include/pulsar/Message.h b/include/pulsar/Message.h index a778660a..b7b3fdd0 100644 --- a/include/pulsar/Message.h +++ b/include/pulsar/Message.h @@ -90,8 +90,15 @@ class PULSAR_PUBLIC Message { * Get string representation of the message * * @return the string representation of the message payload + * + * NOTE: For MSVC with debug mode, return a thread local std::string object to avoid memory allocation + * across DLLs and applications, which could lead to a crash. */ +#if defined(_MSC_VER) && !defined(NDEBUG) + const std::string& getDataAsString() const; +#else std::string getDataAsString() const; +#endif /** * Get key value message. diff --git a/lib/Message.cc b/lib/Message.cc index 84f203f7..46aeb473 100644 --- a/lib/Message.cc +++ b/lib/Message.cc @@ -54,7 +54,15 @@ const void* Message::getData() const { return impl_->payload.data(); } std::size_t Message::getLength() const { return impl_->payload.readableBytes(); } +#if defined(_MSC_VER) && !defined(NDEBUG) +const std::string& Message::getDataAsString() const { + thread_local std::string value; + value = std::string{static_cast(getData()), getLength()}; + return value; +} +#else std::string Message::getDataAsString() const { return std::string((const char*)getData(), getLength()); } +#endif Message::Message() : impl_() {}