Skip to content

[Bug] Message::getDataAsString() will crash with Windows debug library #107

@BewareMyPower

Description

@BewareMyPower

Search before asking

  • I searched in the issues and found nothing similar.

Version

It works for master branch, but you can also use the release here x64-windows-static-Debug in here.

Uncompress it to a directory, e.g. D:\pulsar-cpp-debug.

D:\pulsar-cpp-debug
    bin/pulsar.dll
    lib/pulsar.lib
    include/

Minimal reproduce step

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project("PulsarApp" CXX)

set(PULSAR_ROOT "D:\\pulsar-cpp-debug")
add_executable(PulsarApp main.cc)
include_directories("${PULSAR_ROOT}/include")
target_link_libraries(PulsarApp PRIVATE "${PULSAR_ROOT}/lib/pulsar.lib")
set_property(TARGET PulsarApp PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

main.cc

#include <pulsar/Client.h>
using namespace pulsar;

int main() {
  auto msg = MessageBuilder().setContent("hello").build();
  std::cout << msg.getDataAsString() << std::endl;
  return 0;
}

Then run the following commands in PowerShell

cmake -B build
cmake --build build --config Debug
cp D:\pulsar-cpp-debug\bin\pulsar.dll .
.\build\Debug\PulsarApp.exe

What did you expect to see?

"hello" is printed.

What did you see instead?

image

Anything else?

It looks like to be caused by the combination of static linking of 3rd party dependencies (LINK_STATIC=ON) and the std::string API compatibility.

For example, the following example works well.

#include <assert.h>
#include <pulsar/Client.h>
using namespace pulsar;

int main() {
  auto topic = "topic";
  Client client("pulsar://172.24.101.226:6650");
  Producer producer;
  assert(ResultOk == client.createProducer(topic, producer));
  Consumer consumer;
  assert(ResultOk == client.subscribe(topic, "sub", consumer));
  producer.send(MessageBuilder().setContent("msg").build());
  Message msg;
  assert(ResultOk == consumer.receive(msg));
  // NOTE: it's the same with the `getDataAsString()` implementation
  std::cout << std::string(static_cast<const char*>(msg.getData()), msg.getLength()) << std::endl;
  client.close();
}

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions