我正在使用libjson-c编写应用程序。我已经使用api的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;
}