Comments (4)
I also tried your example like that
// Please do not move the line below as it must outlive the packet
Protocol::MQTT::V5::Property<uint32> maxProp(Protocol::MQTT::V5::PacketSizeMax, 2048);
Protocol::MQTT::V5::Property<Protocol::MQTT::Common::DynamicStringPair> userProp(Protocol::MQTT::V5::UserProperty, Protocol::MQTT::Common::DynamicStringPair("key", "value"));
//Protocol::MQTT::V5::ControlPacket<Protocol::MQTT::V5::CONNECT> packet;
// Check if we have a max packet size property and if not, append one to let the server know our limitation (if any)
//packet.props.append(&maxProp); // It'll fail silently if it already exists
props.append(&userProp); // It'll fail silently if it already exists
Same behaviour
from emqtt5.
For your code, the issue is that you're making a pointer that's pointing to an object that's deleted. The prop
instance in your if
block doesn't survive after the if
block. So the Properties
object in publish
in now referencing a dangling object.
You have two solutions to your issue.
Solution 1: Still use the property that's allocated on the stack, you'll need to move the declaration in the same scope as the usage, like this:
Protocol::MQTT::V5::registerAllProperties();
Network::Client::MQTTv5::Properties properties;
Network::Client::MQTTv5::DynamicBinDataView c_data(correlation_data.length(), (const uint8_t*)correlation_data.c_str());
Protocol::MQTT::V5::Property<Network::Client::MQTTv5::DynamicBinDataView> prop(Protocol::MQTT::V5::CorrelationData, c_data);
if (!correlation_data.empty()) {
if (!properties.append(&prop))
printf("PROP APPEND FAILED!\n");
else
printf("PROP APPEND SUCCESS!\n");
}
client.publish(publish_topic, (const uint8*) publish_message,
strlen(publish_message), retain_published_message, QoS, (uint16)0U, &properties))
This is valid because std::string::c_str
returns a valid zero terminated string if empty (see here). It would be also valid if it returned nullptr
since you're using a view and a view can be null.
The property prop
will be destructed at end of the function scope, so after publish
used it and no pointer is dangling in that case.
Solution 2: Use a property that's allocated on the heap instead
Write like this:
Protocol::MQTT::V5::registerAllProperties();
Network::Client::MQTTv5::Properties properties;
if (!correlation_data.empty()) {
Network::Client::MQTTv5::DynamicBinDataView c_data(correlation_data.length(), (const uint8_t*)correlation_data.c_str());
auto * prop = new Protocol::MQTT::V5::Property<Network::Client::MQTTv5::DynamicBinDataView> (Protocol::MQTT::V5::CorrelationData, c_data, true);
if (!properties.append(prop))
printf("PROP APPEND FAILED!\n");
else
printf("PROP APPEND SUCCESS!\n");
}
client.publish(publish_topic, (const uint8*) publish_message,
strlen(publish_message), retain_published_message, QoS, (uint16)0U, &properties))
Notice the , true
in the end of the Property
constructor that tells the library to delete
the instance when the Properties
is being destructed.
Hope that helps!
from emqtt5.
Sorry for the inconvenience, i'm idiot
Completely forgot about scope and if
from emqtt5.
That's ok, the interface in eMQTT5 is designed for embedded development, so when a pointer is used in the methods signature, it often doesn't expect a heap allocated object (or you have to explicitly tell it that it's a pointer to the heap). Dealing with pointers on the stack that self destruct is not usual for a C++ developer, I can easily understand it's disturbing.
from emqtt5.
Related Issues (15)
- Cannot build with MQTTOnlyBSDSocket set to 0 [Windows build] HOT 7
- Problems including the library HOT 11
- Porting to Renesas MCU without OS HOT 1
- Publisher and Subscriber in the same client HOT 12
- execinfo.h does not exist in alpine linux 3.19 HOT 2
- read properties of received packet HOT 9
- readFrom() return BadData for 4-byte buffer HOT 2
- Linux specific int types HOT 3
- missing functions getEthernetRate and getWIFIRate HOT 9
- eMQTT5 does not install at all HOT 1
- There seems no definition of Platform::free and so on for macOS. HOT 18
- Clean source dependency for readability HOT 6
- No support for unsubscribing HOT 4
- Stuck in eventLoop HOT 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from emqtt5.