json_object_put вызывает сбой кода при использовании с json объекта Api - PullRequest
0 голосов
/ 06 декабря 2018

Я пишу приложение, используя libjson-c.Я использовал json_tokener_parse (), json_object_object_get_ex (), json_object_array_get_idx () для обработки моих json-объектов.

Я также использовал json_object_put для освобождения объектов, но мое приложение вылетает.Я внес изменения так, что функция * _put () используется только один раз в конце функции для всех определенных json_object, но даже это не помогает.Также пытался использовать _put () только для объектных указателей, используемых в func json_tokener_parse (), но даже это вызывает утечку памяти.

Мне потребуется некоторая помощь для правильного использования функции _put () для примера кода ниже.

мой пример кода выглядит примерно так

char *getVal(json_object *queriesObj, char *str){
    json_object *ret;
    bool found;
    const char *Val;
    found = json_object_object_get_ex(queriesObj,str,&ret);
    if(found){
        Val = json_object_get_string(ret);
        return (char *)Val;
    }
    return NULL;
}

void parse_conntrack_input( json_object *jobj,int *flow_count ) {

    //ConnReq *req;
    int exists, i, j;
    json_object *queriesObj1=NULL;
    json_object *queriesObj2=NULL;
    json_object *queriesObj3=NULL;
    json_object *tmpQueries=NULL;
    json_object *tmpQueries1=NULL;
    json_object *jobj1=NULL;
    json_object *jobj2=NULL;
    char *Val=NULL;
    char *buf1=NULL;
    char *buf2=NULL;
    int count;

    exists = json_object_object_get_ex( jobj, "abc", &queriesObj1 );
    if ( FALSE != exists ) {
        buf1=(char *)json_object_get_string(queriesObj1);
        jobj1=json_tokener_parse( buf1 );
        if(buf1){
            free(buf1);
        }
        exists = json_object_object_get_ex( jobj1, "bcd", &queriesObj2 );

        if ( FALSE != exists ) {

             for ( i = 0; i < json_object_array_length( queriesObj2 ); i++) {

                tmpQueries = json_object_array_get_idx( queriesObj2, i );
                buf2=(char *)json_object_get_string(tmpQueries);
                jobj2=json_tokener_parse( buf2 );
                if(buf2){
                    free(buf2);
                }
                exists = json_object_object_get_ex( jobj2, "def", &queriesObj3 );

                if ( FALSE != exists ) {

                    for ( j = 0; j < json_object_array_length( queriesObj3 ) ; j++){
                        tmpQueries1 = json_object_array_get_idx( queriesObj3, j );
                        Val = getVal(tmpQueries1, (char *)"dfg");
                        printf("val is %s",Val);
                        Val = getVal(tmpQueries1, (char *)"rfg");
                        printf("val is %s",Val);
                    }

                }
            }
        }
    }

    return;
}
void main()
{
    json_object *jobj;
    int count=0;
    char *buffer="<some json data ......>";
    jobj = json_tokener_parse( buffer );
    parse_conntrack_input( jobj,&count);

    json_object_put(jobj);
    return;
 }
...