Giter Club home page Giter Club logo

Comments (4)

turbosapiens avatar turbosapiens commented on June 24, 2024

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.

X-Ryl669 avatar X-Ryl669 commented on June 24, 2024

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.

turbosapiens avatar turbosapiens commented on June 24, 2024

Sorry for the inconvenience, i'm idiot
Completely forgot about scope and if

from emqtt5.

X-Ryl669 avatar X-Ryl669 commented on June 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.