Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/pulsar/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions lib/Message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(getData()), getLength()};
return value;
}
#else
std::string Message::getDataAsString() const { return std::string((const char*)getData(), getLength()); }
#endif

Message::Message() : impl_() {}

Expand Down