-
Notifications
You must be signed in to change notification settings - Fork 17
call yajl_gen_free() to prevent memory leaks #19
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
Conversation
anjohnson
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for finding and fixing these, I agree with your changes. I will merge this PR (and the others now waiting) as soon as I get a chance.
| flag = call; \ | ||
| if (flag != yajl_gen_status_ok) { \ | ||
| errlogSevPrintf(errlogMinor, "caPutJsonLog: JSON generation error\n"); \ | ||
| yajl_gen_free(handle); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor comment here: this seems weird in the macro to reference handle which is not in fact passed to the macro.
Admittedly I don't know of an easy way to fix it; the call parameter can be functions with variable numbers of arguments. But this looks a bit weird to have a reference in a macro that isn't actually passed to the macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, not a big deal, I've seen code do that.
The macro will fail at compile time if used in function that does not specify 'handle'.
OTOH, moving the macro into the function is also an option; this way it "signals" to the devs that it is "meant" to be used in that function only (macro will nevertheless remain defined even outside the function).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In c++ the conventional way to avoid this sort of resource leak would be to wrap the handle in a class with yajl_gen_free() in a destructor. eg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In c++ the conventional way to avoid this sort of resource leak would be to wrap the handle in a class with yajl_gen_free() in a destructor. eg.
That looks clean and safe. Since this code has no such wrapper class at the moment is it worth introducing it now for purpose at hand? Maybe.
If the ugliness of accessing handle from the macro is what we are solving, wouldn't passing it in as the first arg to the macro be acceptable?
#define CALL_YAJL_FUNCTION_AND_CHECK_STATUS(handle, flag, call) \
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that @mdavidsaver 's solution seems nicer to me; it's a pretty thin wrapper class to guarantee that we are going to fix this and other potential memory issues. Passing the handle to the macro as well feels more like a bandaid.
|
It was not initially clear to me from reading this thread, but from listening to @ttkorhonen during the recent collaboration meeting I have gathered that the leak being plugged here is on the "success" path, and is not limited to @anjohnson Are you the one to merge this PR? (as with the original #4) |
Maybe not? I don't see a reason to hold up plugging this runtime memory leak while waiting on feature PRs. I think it would be nice if someone could come back and make this .cpp file look more like c++ code, but again not a good reason to wait. It appears that I have permission to apply this fix, so I will. |
Running
valgrind --leak-check=full --show-leak-kinds=all caPutJsonLogTestit was observed:After this PR change was applied
valgrinddid not report and more leaks fromCaPutJsonLogTask::buildJsonMsg.Tested with epics base 7.0.7.