-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please provide more precise informations about when to call json_object_put #642
Comments
It's an interesting topic. And I sorted out the related interfaces.
So, it looks like Maybe we should provide more detailed messages about these in Readme. |
But, it is indeed a question. if we call |
No, you MUST NOT "visit" obj2 because as soon as you call json_object_put(obj1) obj2 is no longer valid. Trying to use it anyway is a use-after-free error, exactly as if you were directly to do |
The tokener is independent from the json objects. You create the tokener, use it once, or multiple times, and free it when you are done with it. It is not tied to the lifetime of the objects it creates while parsing text, the objects live on. Typically, you create a tree of json objects, either by parsing text with a tokener, or by creating-and-adding them one-by-one. Typically, every object in the tree will have one reference, from it's parent. When you are done with the tree of objects, you You can take a reference to a sub-section of the object tree and use it for a non-trivial amount of time, perhaps after the original object tree was freed, by EDIT: I hereby license this comment text under the "CC0" license, to be used as freely as possible. |
@fralbo, the Stackoverflow page you linked to explicitly links to the 0.10 docs, so of course you're going to see the old info. You should start at http://json-c.github.io/json-c/, and look at the 0.14 (soon to be 0.15) docs instead, there have been a TON of improvements, including specifying when reference counts are adjusted. Of course, there's always room for improvement.
|
Yes, agree. It is a wrong action. But many users don not know this. It may lead them to free or visit the pointer and the valgrind throw an exception finally. They may be confused for this. So we need to emphasize these in doc. |
ok,
What about `json_object_from_file()`?
I currently read the file by myself and pass the string to
`json_tokener_parse` to build the json tree.
I guess that `json_object_from_file` can do both.
But what to do after?
I suppose that it open/close the file....
And the returned object ??
Is it ok to just call `json_tokener_free`?
|
@fralbo , you can't call json_tokener_free() when you don't have a json_tokener! That is just basic C usage, please actually look at the types of the APIs you are referring to. You free the returned json_object with json_object_put(), just like any other json_object. Unless otherwise specified (i.e. only |
@hawicz yes of course, sorry for the mistake. |
With the updated docs on the functions, and the additional detail I added to README.md, I'm considering this done. |
Hello,
It's difficult to determine when to call
json_object_put
afterjson_obect_xxx
calls.I found tons of questions about this and all examples and tutorials I found really don't care about correctly freeing the memory.
And answers and not allways clear as it seems that doubts are widely shared.
One example about
json_object_parse_ex
in your documentation, you effectively specify that an object is returned so we can logically think that a call tojson_object_put
is necessary but a clearer notice would be fine all the more the given code snippet doesn't mentionjson_object_put
call. Maybejson_object_put
documentation could give the list of the functions supposed to need this call.I understand it's maybe not so simple as some functions transfer ownership and so, if I well understand because it's also not so obvious, doesn't wait from freeing the object from theirself.
What
json_tokener_free
do ??Some functions increment ownership such as
json_object_get
is it the only one?Can
json_object_get
be called other function, likejson_tokern_parse
for example?And so is there an
json_object_count
to calljson_object_put
until 0?Often, functions which doesn't change increment count specify that there is no need to call
json_object_put
except for special situation.But the ones which need it don't tell anything such as
json_object_get
even if it souds obvious for you.And itseems that the documentation refers to 0.10 version.
Thanks in advance.
The text was updated successfully, but these errors were encountered: