diff --git a/src/iclient_persistence.cpp b/src/iclient_persistence.cpp index 057b4d7..5f70531 100644 --- a/src/iclient_persistence.cpp +++ b/src/iclient_persistence.cpp @@ -81,8 +81,10 @@ int iclient_persistence::persistence_get(void* handle, char* key, try { if (handle && key && buffer && buflen) { auto sv = static_cast(handle)->get(key); - *buffer = const_cast(sv.data()); - *buflen = (int) sv.length(); + size_t n = sv.length(); + *buffer = static_cast(MQTTAsync_malloc(n)); + memcpy(*buffer, sv.data(), n); + *buflen = int(n); return MQTTASYNC_SUCCESS; } } diff --git a/src/samples/async_publish.cpp b/src/samples/async_publish.cpp index 7581cfd..1ab8108 100644 --- a/src/samples/async_publish.cpp +++ b/src/samples/async_publish.cpp @@ -8,6 +8,7 @@ // The sample demonstrates: // - Connecting to an MQTT server/broker // - Publishing messages +// - Default file persistence // - Last will and testament // - Using asynchronous tokens // - Implementing callbacks and action listeners @@ -42,6 +43,7 @@ using namespace std; const std::string DFLT_SERVER_ADDRESS { "tcp://localhost:1883" }; const std::string DFLT_CLIENT_ID { "async_publish" }; +const std::string PERSIST_DIR { "./persist" }; const string TOPIC { "hello" }; @@ -127,7 +129,7 @@ int main(int argc, char* argv[]) clientID = (argc > 2) ? string(argv[2]) : DFLT_CLIENT_ID; cout << "Initializing for server '" << address << "'..." << endl; - mqtt::async_client client(address, clientID); + mqtt::async_client client(address, clientID, PERSIST_DIR); callback cb; client.set_callback(cb); diff --git a/src/samples/sync_publish.cpp b/src/samples/sync_publish.cpp index 974494a..dda19a7 100644 --- a/src/samples/sync_publish.cpp +++ b/src/samples/sync_publish.cpp @@ -100,7 +100,7 @@ public: << key << "']" << std::endl; std::string str; for (const auto& b : bufs) - str += b.str(); + str.append(b.data(), b.size()); // += b.str(); store_[key] = std::move(str); }