[SR-Users] How to avoid mem leak when use Kamailio Json module

dongwf dongwflj at 163.com
Wed Nov 5 11:23:34 CET 2014


One more question based the last mail, if I want to return the j object pointer via PV to script( created as struct json_object *j = json_tokener_parse(json_s.s);), how can I achieve that? Then I can put the pointer back to another exported api and release the pointer via json_object_put, thanks!






At 2014-11-05 17:54:46, "dongwf" <dongwflj at 163.com> wrote:

Hi:
I am trying to use json module in script with json_get_field exported api, I found in it's implementation, the api will call struct json_object *j = json_tokener_parse(json_s.s); but didn't call json_object_put for this j object, then the object will leak. So my question is how to release the new j object? We can't release in json_get_field(), that will cause the returned string become to rubbish, thanks!
Below is the api's code in module\json\json_funcs.c:
int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst)
{
  str json_s;
  str field_s;
  pv_spec_t *dst_pv;
  pv_value_t dst_val;


if (fixup_get_svalue(msg, (gparam_p)json, &json_s) != 0) {
LM_ERR("cannot get json string value\n");
return -1;
}


if (fixup_get_svalue(msg, (gparam_p)field, &field_s) != 0) {
LM_ERR("cannot get field string value\n");
return -1;
}
dst_pv = (pv_spec_t *)dst;
struct json_object *j = json_tokener_parse(json_s.s);


if (is_error(j)) {
LM_ERR("empty or invalid JSON\n");
return -1;
}


char *value = (char*)json_object_to_json_string(json_object_object_get(j, field_s.s));


dst_val.rs.s = value;
dst_val.rs.len = strlen(value);
dst_val.flags = PV_VAL_STR;
dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);


return 1;
}




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sip-router.org/pipermail/sr-users/attachments/20141105/e369206a/attachment.html>


More information about the sr-users mailing list