On Thu, May 20, 2010 at 10:01 AM, Petri Lehtinen <
pe...@digip.org> wrote:
> When you build up an object by hand, you probably do something like
> this:
>
> json_t *object = json_object();
> json_object_set(object, "foo", json_integer(15));
>
> This leaks memory, as json_integer() returns a new reference to an
> integer value, and json_object_set() increases its reference count.
Petri, I know this is documented, but the examples you present
here in your email response are very clear and lucid; and
this is a type of mistake I'm sure is very easy for people to
unknowingly make.
Perhaps you can incorporate some of this extra explanation
into the docs to make it even clearer?
The current docs mention that some functions may "steal"
references, but leaves it to the reader to find them. Aside
from perhaps showing an example of a bad practice,
json_object_set( object, "foo", json_integer(15))
and the good practice,
json_object_set_new( object, "foo", json_integer(15))
it may also be nice to just list all the "stealing" functions
and the non-stealing counterparts in one place in the
Reference Counting section, e.g.,
json_object_set / json_object_set_new
json_object_set_nocheck / json_object_set_new_nocheck
json_array_set / json_array_set_new
json_array_append / json_array_append_new
json_array_insert / json_array_insert_new
Thanks
--
Deron Meranda
http://deron.meranda.us/