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
memory leak #423
Comments
|
https://stackoverflow.com/questions/14879994/how-to-free-json-object/63289941#63289941 Some good info on this there. You have to understand how the library works.
So if you get all the way down to making a char *str for a string field, that field doesn't actually store the string, the original object returned from This is the reason that you can't just use a normal free() and expect things to work as if it was a char array or something. To be safe, don't use the Beyond that, to safely close everything just do:
You should only need to do that once, but the library may change. |
This has some terrible advice. Avoiding json_tokener_parse_ex() because you don't want to free the tokener isn't "being safe", it's being lazy. (which could be perfectly fine, if you don't need the fine grained control that having a tokener object gives you) Calling json_object_put in a loop implies that your program has completely lost track of what it has allocated and incremented reference counts for, and is a great way to end up using already freed memory, including calling json_object_put() on an object that is no longer valid. |
The tokener object has no options. You didn't see that the loop is based on the put() return value. Quit fighting, that's how you free the objects this library creates. |
The tokener object does have operations that can be performed on it to either control parsing or obtain the results of parsing. Please see the functions at http://json-c.github.io/json-c/json-c-current-release/doc/html/json__tokener_8h.html for details. Actually, I did see that your loop checks the return value, and I still maintain that calling it in that way is a bad idea. Though you might manage to cause the "orig_object" to be freed, calling json_object_put in a loop is dangerously sloppy and using such an approach in any non-trivial program will quite likely lead to use-after-free errors (and for simple programs it's entirely unnecessary to loop). |
I guess you just don’t know how the compiler works.
Andrew
…On Fri, Aug 7, 2020 at 12:22 PM Eric Hawicz ***@***.***> wrote:
The tokener object *does* have operations that can be performed on it to
either control parsing or obtain the results of parsing. Please see the
functions at
http://json-c.github.io/json-c/json-c-current-release/doc/html/json__tokener_8h.html
for details.
Actually, I *did* see that your loop checks the return value, and I still
maintain that calling it in that way is a bad idea. Though you might manage
to cause the "orig_object" to be freed, calling json_object_put in a loop
is dangerously sloppy and using such an approach in any non-trivial program
will quite likely lead to use-after-free errors (and for simple programs
it's entirely unnecessary to loop).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#423 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFVCSISME7MEVCX2HDHQWTR7RA53ANCNFSM4FER6HJA>
.
|
How to delete objects that were received by json_object_new_string? If you can not delete, will there be a memory leak or not?
The text was updated successfully, but these errors were encountered: