-
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
json_tokener_parse memory leak? #685
Comments
Can you provide a self-contained code sample? There might be some confusion: you do need to |
Sorry for the late response, busy busy... Below I added a codesample where I don't clean the json at that snippet, but when I try to add the It was indeed a bit confusing, but makes sense now. code snippet: json_object *json = json_tokener_parse(message); // message is a json string that comes from rabbitmq
int rows, cols;
rows = getNumberOfRows(json);
cols = getNumberOfCols(json);
InputParameters input;
MatrixInputParameters matrix[rows][cols];
parseMessage(json, rows, cols, &input, matrix);
OutputValues output[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
callModel(&input, &matrix[i][j], &output[i][j]);
}
}
char* outMessage = createMessage(rows, cols, &input, output);
publishMessage(conn, EVENT_QUEUE, outMessage);
free(outMessage);
free(input.simulationUid); |
A call to json_object_put() at the end of this code block seems like it would be appropriate. There should be no need to increase the refcount. If that is the only call to _put() in your code, then the next thing I would suspect is something stomping on memory; running things through a debugger often rearranges things enough to change the behavior for bugs like that. |
I suspect it might be related to json manipulation in |
This is what valgrind shows:
|
Ok, so json_tokener parsed and allocated an array which didn't get freed. Are you calling json_tokener_put() on the object returned from json_tokener_parse()? If not, then of course you need to do so. |
What is the prototype of I am using |
For 0.13.1 specifically, see http://json-c.github.io/json-c/json-c-0.13.1/doc/html/json__object_8h.html#afabf61f932cd64a4122ca8092452eed5, although it's the same in every version of json-c:
For additional details when parsing you can see the json_tokener_parse_ex() docs or for just general json-c usage there's a tutorial over at: https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/index.md |
@hawicz Thanks, It worked for me! |
Describe the bug
I'm using JSON C to parse a relatively large JSON.
I cleaned everything up but when running through Leaks (xcode memoryleak detector), I still get memory leaks.
Steps To Reproduce
I just parse my json with
json_object *json = json_tokener_parse(message);
where message is the json string.Then I do calculations, parse it back to a json and put it on a rabbitmq.
I clean everything up I create, and according to the docs I don't need to do manual cleanup for the parse method. However if I run my program through Leaks, I get the following output (see screenshot). Before I did my own cleanup I had 5x the entries, but after doing that all that is left are entries from the json_tokener_parse method.
Version and Platform
The text was updated successfully, but these errors were encountered: