C - 复制整数

时间:2017-04-12 12:22:33

标签: c

所以我使用的是jansson库,我想设置一个值: json_object_set_new(event," error_code",json_integer(response-> error_code));

问题在于,我需要free response struct,但仍然使用我在json_object_set_new中设置的值。

我应该从response->error_code复制值吗?我该怎么做?

某些背景信息:

plugin_response: {
    if(!response->message && response->error_code == 0) {
            response->error_code = JANUS_AUDIOBRIDGE_ERROR_UNKNOWN_ERROR;
      if(response->error_cause == NULL) {
              response->error_cause = g_strdup_printf("%s", "Invalid response");
      }
    }

        json_t *event = json_object();
        if(!response->message && response->error_code != 0) {
            /* Prepare JSON error event */
            json_object_set_new(event, "audiobridge", json_string("event"));
            json_object_set_new(event, "error_code", json_integer(response->error_code));
            json_object_set_new(event, "error", json_string(response->error_cause));
        } else {
          event = json_deep_copy(response->message);
    }

        if(root != NULL)
            json_decref(root);
        if(jsep != NULL)
            json_decref(jsep);
        g_free(transaction);
        g_free(response);



return janus_plugin_result_new(JANUS_PLUGIN_OK, NULL, event);
}

编辑:我也在使用GLib,所以如果有一个漂亮的方法,我都是耳朵。

1 个答案:

答案 0 :(得分:0)

哦,我读了这个This article,我明白了。我不需要复制传递给json_integer()的整数,因为它不是指针而且与结构整数完全隔离。