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

Daniel-Constantin Mierla miconda at gmail.com
Wed Nov 5 14:35:40 CET 2014


Hello,

I pushed a fix that should release the json object:

-
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=def8d2600422a442b971c204df98594b0dfdaacd

dst_pv->setf() should copy (clone) of the string value and then the object can be unreferenced.


If works ok, then I will backport -- I didn't have any server with json
library at hand to give it a minimum test.

Cheers,
Daniel

On 05/11/14 11:23, dongwf wrote:
> 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;
>     }
>
>
>
>
>
>
>
>
> _______________________________________________
> SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
> sr-users at lists.sip-router.org
> http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users

-- 
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Kamailio Advanced Training, Nov 24-27, Berlin - http://www.asipto.com

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


More information about the sr-users mailing list