Search code examples
cvisual-c++mqttpaho

Which variable cause segmentation fault?


Trying to debug unhandled exception in MQTT Paho library. Can't figure out in which exact variable rises segmentation fault. Variables m and ma are not NULL. Not sure what does {...} means in m->c->connect_state.

How to know which exact variable raised the problem?

How to solve this problem?

enter image description here

Function code in MQTTClient.c:

int MQTTClient_setCallbacks(MQTTClient handle, void* context, MQTTClient_connectionLost* cl,
                                                        MQTTClient_messageArrived* ma, MQTTClient_deliveryComplete* dc)
{
    int rc = MQTTCLIENT_SUCCESS;
    MQTTClients* m = handle;

    FUNC_ENTRY;
    Thread_lock_mutex(mqttclient_mutex);

    if (m == NULL || ma == NULL || m->c->connect_state != NOT_IN_PROGRESS)
        rc = MQTTCLIENT_FAILURE;
    else
    {
        m->context = context;
        m->cl = cl;
        m->ma = ma;
        m->dc = dc;
    }

    Thread_unlock_mutex(mqttclient_mutex);
    FUNC_EXIT_RC(rc);
    return rc;
}

This code is from official Paho library: https://github.com/eclipse/paho.mqtt.c/blob/master/src/MQTTClient.c And why they make such assignment: MQTTClients* m = handle;


Solution

  • How to know which exact variable raised the problem?

    Replace

    if (m == NULL || ma == NULL || m->c->connect_state != NOT_IN_PROGRESS)
        rc = MQTTCLIENT_FAILURE;
    

    temporarily with

    if (m == NULL)
        rc = MQTTCLIENT_FAILURE;
    
    else if (ma == NULL)
        rc = MQTTCLIENT_FAILURE;
    
    else if (m->c->connect_state != NOT_IN_PROGRESS)
        rc = MQTTCLIENT_FAILURE;
    

    Now you can see which variable is causing the problems.