1

I am quite new in C, and I am using the json-c library. I am completely sure that the problem I have is with json_object_get_string, because if I don't use it and put a string manually in my structure it works in valgrind without any memory leak.

I put a reduced version to make it clearer.

struct Example {
    char *id;
    char *name;
    char *logPath;
};

struct Examples {
    struct Example *examples;
    size_t size;
};

struct Examples list() {

    size_t i, count = 0;
    struct Example *examples = NULL;

    for (i = 0; i < 59; i++) {

        json_object *root_obj = json_object_from_file("path.json");
        json_object *jID;
        json_object *jName;
        json_object *jLogPath;

        if (json_object_object_get_ex(root_obj, "Name", &jName) ==
                TRUE &&
            json_object_object_get_ex(root_obj, "LogPath", &jLogPath) ==
                TRUE &&
            json_object_object_get_ex(root_obj, "ID", &jID) == TRUE) {
            char *id = strdup(json_object_get_string(jID));
            char *name = strdup(json_object_get_string(jName));
            char *logPath = strdup(json_object_get_string(jLogPath));
            json_object_put(root_obj);
            count++;

            struct Examples *tmpExamples = realloc(examples, count * sizeof(struct Example));
            if (tmpExamples == NULL) {
                if (examples) {
                    free(examples);
                }
                die("Realloc");
            }
            struct Example example = { id, name, logPath };

            examples = tmpExamples;
            examples[i] = container;
    }

    struct Examples examplesList = { examples, count };

    return examplesList;
}

I have tried to free the variables that I have assigned with strdup after adding it to the struct, but then I lose the real value of the string. I don't really understand what happens.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
jcarlosweb
  • 846
  • 1
  • 14
  • 30
  • 1
    I had to read the whole spec to discover that a `json_object` is discarded by calling `json_object_put()`... What a stupid idea. This library is so painfully verbose. – chqrlie Jun 11 '22 at 14:06
  • 1
    `json_object_get_string()` does return a `const char *` so you should indeed use `strdup()` to make a copy. Are you sure you free these copies in the calling code? Why not post a complete program that produces the leak? – chqrlie Jun 11 '22 at 14:08
  • Thanks, I just solved it, I put it as an answer. – jcarlosweb Jun 11 '22 at 14:29
  • You should look at the valgrind output and find out **what** is not being freed and where it was allocated. Please also consider posting a [mcve]. You need a buildable program, but you **do not** need to read an object from a file or do it 60 times in a row or get 3 different strings out of it. *One* hardcoded json object, *one* call to `json_object_get_string`. – n. m. could be an AI Jun 11 '22 at 14:35

1 Answers1

0

In the end I have not complicated with so much char pointer and the chars of the struct I have put it as char array if I can name it that way

struct Example {
    char id[64];
    char name[30];
    char logPath[165];
};

struct Example example;
strcpy(example.id, json_object_get_string(jID));
strcpy(example.name, json_object_get_string(jName));
strcpy(example.logPath, json_object_get_string(jLogPath));
json_object_put(root_obj);
jcarlosweb
  • 846
  • 1
  • 14
  • 30