MQTT publish exception(Access violation reading location)

0

When I am publishing message via mqtt::publish(), I am getting below exception.

Exception thrown at 0x00007FF68471021C in xxxxxxxxxxxxxxxxxxxxx.exe: 0xC0000005: Access violation reading location 0x0000000000000008

enter image description here

Part of code where I am publishing my message :

mqtt_client.h

class mqtt_client : public virtual mqtt::callback
{
private:    
    static const std::uint16_t                                      connect_timeout = 10000;
    static inline                                               mqtt_client *instance = nullptr;
    std::string                         uri;
        uint16_t                                                        mqtt_timeout;
        std::string                         client_id;
        std::string                         topic;
        std::string                         topic_in;
        std::string                         topic_out;
    std::mutex                          transmit_queue_mutex;
    std::atomic_bool                                            transmit_in_progress{ false };
    std::atomic_bool                                            connection_established{ false }, connection_in_progress{ false };
    std::queue<std::string>                                     transmit_queue;
    std::unique_ptr<mqtt::async_client>                             client;
public:
        mqtt_client();
    ~mqtt_client();
    inline const static uint8_t                                     qos = 2;
    static mqtt_client                                              *get_instance(void);
    bool                                                            connect(void);
    bool                                                            send_data(const std::string& data);
    mqtt_client(mqtt_client &&obj) = delete;
    mqtt_client& operator=(const mqtt_client&) = delete;
    mqtt_client& operator=(mqtt_client&&) = delete;
};

mqtt_client.cpp

mqtt_client *mqtt_client::get_instance(void)
{
    try {
        if (mqtt_client::instance == nullptr)
            mqtt_client::instance = new mqtt_client();
        return mqtt_client::instance;
    }
    catch (const std::exception& ex) {
        mqtt_client::instance = nullptr;
        return mqtt_client::instance;
    }
}

bool mqtt_client::connect(void)
{
    try 
    {
        this->client = std::make_unique<mqtt::async_client>(this->uri, this->client_id);
        this->client->set_callback(*this);
        mqtt::connect_options conn_opts;
        conn_opts.set_clean_session(false);
        conn_opts.set_will_message(mqtt::message(this->topic_out, "Disconnected", 12));
        conn_opts.set_automatic_reconnect(true);
        mqtt::token_ptr connect_token = this->client->connect(conn_opts);
        connect_token->wait();
        return true;
    }
    catch (const mqtt::exception& ex) 
    {
        std::string stre = std::string("Error in MQTT connection. Error : ") + ex.what() 
               + std::string(" [ ") + std::to_string(ex.get_reason_code()) + std::string(" ] ");
        LOGE << stre;
#ifdef _DEBUG
        std::cerr << "Error: " << ex.what() << " ["
            << ex.get_reason_code() << "]" << std::endl;
#endif 
        return false;
    }
    catch (const std::exception& ex) 
    {
        std::string stre = std::string("Error while connecting to MQTT. Error : ") + ex.what();
        LOGE << stre;
#ifdef _DEBUG
        std::cout << stre << std::endl;
#endif 
        return false;
    }
}

bool mqtt_client::send_data(const std::string& data)
{
    try {
        {
            std::lock_guard<std::mutex> lock { this->transmit_queue_mutex };
            this->transmit_queue.push(data);
        }
        if(not this->transmit_in_progress.load())
            this->transmit_message();
        return true;
    }
    catch (const mqtt::exception& ex) 
    {
        std::string stre = std::string("Error in sending data. Error : ") + ex.what()
                + std::string(" [ ") + std::to_string(ex.get_reason_code()) + std::string(" ] ");
        LOGE << stre;
#ifdef _DEBUG
        std::cerr << "Error: " << ex.what() << " ["
            << ex.get_reason_code() << "]" << std::endl;
#endif 
        return false;
    }
    catch (const std::exception& ex)
    {
        std::string stre = std::string("Error in sending data. Error : ") + ex.what();
        LOGE << stre;
#ifdef _DEBUG
        std::cout << stre << std::endl;
#endif 
        return false;
    }
}

void mqtt_client::transmit_message(void)
{
    try {
        std::string data;
        {
                std::lock_guard<std::mutex> lock{ this->transmit_queue_mutex };
                if (this->transmit_queue.empty())
                        return;
                data = this->transmit_queue.front();
        }
        mqtt::message_ptr msg = mqtt::make_message(this->topic_out, data.data(), data.length(), mqtt_client::qos, false);
        this->transmit_in_progress.store(true);
        this->client->publish(msg);
    }
    catch (const mqtt::exception& ex) 
    {
        std::string stre = std::string("Error in transmitting message. Error : ") + ex.what()
                + std::string(" [ ") + ex.get_reason_code_str() + std::string(" ] ");
        LOGE << stre;
#ifdef _DEBUG
        std::cerr << "Error: " << ex.what() << " ["
            << ex.get_reason_code_str() << "]" << std::endl;
#endif 
    }
    catch (const std::exception& ex) 
    {
        std::string stre = std::string("Error in transmitting message. Error : ") + ex.what();
        LOGE << stre;
#ifdef _DEBUG
        std::cout << stre << std::endl;
#endif 
    }
}

This error is occurring in Windows Visual Studio. I am using same code in one project written using Netbean IDE for linux(Raspberry Pi). mqtt is installed in Raspberry using apt get .There it is working without any issue.

OS : Windows 10 Home

Bit : x64

Visual Studio : Microsoft Visual Studio Community 2017

vcpkg : paho-mqttpp3

c++
windows
visual-studio
c++17
mqtt
asked on Stack Overflow Jul 1, 2020 by S.K. • edited Jul 2, 2020 by S.K.

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0