0

在每个偶数索引上,我需要通过 MQTT 发送构造的 json,并且必须在发送后清除 json,并且必须再次构造 json,同样,我使用以下脚本,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#include <jansson.h>

char *jsonString;

void sendMQTT(char *jsonString){
    
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;

    MQTTClient_create(&client, "tcp://localhost:1883", "client-pub",
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    printf("Inside MQTT :: %s\n", jsonString);
    pubmsg.payload = jsonString;
    pubmsg.payloadlen = (int)strlen(jsonString);   
    pubmsg.qos = 1;
    pubmsg.retained = 0;
    
    MQTTClient_publishMessage(client, "mqtt-ex", &pubmsg, &token);
    rc = MQTTClient_waitForCompletion(client, token, 10000L);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);

}


int main() {  
    json_t *root = json_object();
    char myNum[10] = {10, 20, 10, 40, 10, 60, 10, 80, 10, 100};
    
    for(int i=0; i<10;i++)
    {   
        int json_size = json_object_size(root);
        printf("json_size %d\n", json_size);
        if(i%2==0 && json_size !=0)
        {    
            sendMQTT(jsonString);

            free(jsonString);
            json_decref(root);
            json_t *root = json_object();
        }

    char *key= (char*)malloc(8);
    snprintf(key, sizeof(key), "%d", myNum[i]);

    json_object_set_new( root, key, json_integer(i));      
    
    char *jsonString= (char*)malloc(100);
    jsonString = json_dumps(root, 0);
    printf("json_string :!!:  %s\n", jsonString);
    free(key);
  }  
}

然而,当我编译和执行相同的,

gdb ./editing_file
GNU gdb (Ubuntu 8.2-0ubuntu1~18.04) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./editing_file...done.
(gdb) run
Starting program: /home/sam/workspace_folder/sample/editing_file 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
json_size 0
json_string :!!:  {"10": 0}
json_size 1
json_string :!!:  {"10": 0, "20": 1}
json_size 2
Inside MQTT :: (null)

Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:62
62  ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
(gdb) where
#0  __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:62
#1  0x0000555555554f61 in sendMQTT (jsonString=0x0) at editing_file.c:29
#2  0x0000555555555093 in main () at editing_file.c:51
(gdb) 

如何解决这个问题?上述过程有什么问题以及如何纠正?

来自评论的建议

根据评论的建议,上面的代码已修改为,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#include <jansson.h>

char *jsonString;


void sendMQTT(char *jsonString){
    
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;

    MQTTClient_create(&client, "tcp://localhost:1883", "client-pub",
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    printf("Inside MQTT :: %s\n", jsonString);
    pubmsg.payload = jsonString;
    pubmsg.payloadlen = (int)strlen(jsonString);   
    pubmsg.qos = 1;
    pubmsg.retained = 0;
    
    MQTTClient_publishMessage(client, "mqtt-ex", &pubmsg, &token);
    rc = MQTTClient_waitForCompletion(client, token, 10000L);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);

}


int main() {  
    json_t *root = json_object();
    char myNum[10] = {10, 20, 10, 40, 10, 60, 10, 80, 10, 100};
    
    for(int i=0; i<10;i++)
    {   
        int json_size = json_object_size(root);
        if(i%2==0 && json_size !=0)
        {    
            sendMQTT(jsonString);
            json_object_clear(root);

            jsonString = json_dumps(root, 0);
            printf("Inside for ===%s\n", jsonString);            
        }
        

    char *key= (char*)malloc(8);
    snprintf(key, sizeof(key), "%d", myNum[i]);
    
    json_object_set_new( root, key, json_integer(i));      
    jsonString = json_dumps(root, 0);
  }  
}

并能够达到预期的结果,即

 gdb ./editing_file
GNU gdb (Ubuntu 8.2-0ubuntu1~18.04) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./editing_file...done.
(gdb) run
Starting program: /home/sam/workspace_folder/sample/editing_file     [Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Inside MQTT :: {"10": 0, "20": 1}
Inside for ==={}
Inside MQTT :: {"10": 2, "40": 3}
Inside for ==={}
Inside MQTT :: {"10": 4, "60": 5}
Inside for ==={}
Inside MQTT :: {"10": 6, "80": 7}
Inside for ==={}
[Inferior 1 (process 20590) exited normally]
4

0 回答 0