Свободная память всех объектов json_objects из root (включая root)

#c #json-c

Вопрос:

У меня есть этот код:

     struct json_object* parsed_json;
    struct json_object* portInConfig;
    struct json_object* masterInConfig;
    struct json_object* mapInConfig;
    struct json_object* managerPasswdInConfig;
    struct json_object* adminPasswdInConfig;
    struct json_object* modPasswdInConfig;
    struct json_object* guardPasswdInConfig;
    struct json_object* trustedPasswdInConfig;
    struct json_object* serverNameInConfig;
    struct json_object* team1NameInConfig;
    struct json_object* team2NameInConfig;
    struct json_object* team1ColorInConfig;
    struct json_object* team2ColorInConfig;
    struct json_object* gamemodeInConfig;

    parsed_json = json_object_from_file("config.json");
    if (json_object_object_get_ex(parsed_json, "port", amp;portInConfig) == 0) {
        printf("Failed to find port number in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "master", amp;masterInConfig) == 0) {
        printf("Failed to find master variable in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "map", amp;mapInConfig) == 0) {
        printf("Failed to find map name in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "manager_password", amp;managerPasswdInConfig) == 0) {
        printf("Failed to find manager password in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "admin_password", amp;adminPasswdInConfig) == 0) {
        printf("Failed to find admin password in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "moderator_password", amp;modPasswdInConfig) == 0) {
        printf("Failed to find moderator password in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "guard_password", amp;guardPasswdInConfig) == 0) {
        printf("Failed to find guard password in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "trusted_password", amp;trustedPasswdInConfig) == 0) {
        printf("Failed to find trusted password in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "server_name", amp;serverNameInConfig) == 0) {
        printf("Failed to find server name in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "team1_name", amp;team1NameInConfig) == 0) {
        printf("Failed to find team1 name in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "team2_name", amp;team2NameInConfig) == 0) {
        printf("Failed to find team2 name in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "team1_color", amp;team1ColorInConfig) == 0) {
        printf("Failed to find team1 color in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "team2_color", amp;team2ColorInConfig) == 0) {
        printf("Failed to find team2 color in confign");
        return -1;
    }
    if (json_object_object_get_ex(parsed_json, "gamemode", amp;gamemodeInConfig) == 0) {
        printf("Failed to find gamemode in confign");
        return -1;
    }
    port                      = json_object_get_int(portInConfig);
    master                    = json_object_get_int(masterInConfig);
    const char* managerPasswd = json_object_get_string(managerPasswdInConfig);
    const char* adminPasswd   = json_object_get_string(adminPasswdInConfig);
    const char* modPasswd     = json_object_get_string(modPasswdInConfig);
    const char* guardPasswd   = json_object_get_string(guardPasswdInConfig);
    const char* trustedPasswd = json_object_get_string(trustedPasswdInConfig);
    char*       serverName    = (char*) json_object_get_string(serverNameInConfig);
    char*       team1Name     = (char*) json_object_get_string(team1NameInConfig);
    char*       team2Name     = (char*) json_object_get_string(team2NameInConfig);
    uint8       team1Color[3];
    uint8       team2Color[3];
    uint8       gamemode = json_object_get_int(gamemodeInConfig);
    for (int i = 0; i < 3;   i) {
        team1Color[i] = json_object_get_int(json_object_array_get_idx(team1ColorInConfig, i));
        team2Color[i] = json_object_get_int(json_object_array_get_idx(team2ColorInConfig, i));
    }
    uint8 mapArrayLen = json_object_array_length(mapInConfig);
    char  mapArray[mapArrayLen][64];

    for (int i = 0; i < mapArrayLen;   i) {
        uint8 stringLen = json_object_get_string_len(json_object_array_get_idx(mapInConfig, i));
        if (stringLen > 64) {
            continue;
        }
        memcpy(mapArray[i], json_object_get_string(json_object_array_get_idx(mapInConfig, i)), stringLen   1);
    }

    json_object_put(parsed_json);
 

Проблема, с которой я сталкиваюсь, заключается в том, что, когда я пытаюсь освободить объект
parsed_json, он освобождает не все объекты.

Если бы у меня было это в то время как цикл вальгринд кричал бы, что я освобождаю память, я не должен.

Каков способ освободить все объекты (без утечки памяти), не освобождая при этом память, которую я не должен освобождать ?

Ответ №1:

На самом деле у этого нет никакой утечки памяти.

Проблема заключалась в том, как я хранил строку. При передаче его в функцию он больше не освобождал память.

Комментарии:

1. Через несколько дней, если сможете, пожалуйста, вернитесь и отметьте это как решение. Спасибо.

2. Да, я могу сделать это завтра.