Work on libgdata is progressing nicely, and I've started on porting evolution-data-server to the shiny new calendar and contacts services provided by libgdata. One idea I recently had was to use the G_GNUC_WARN_UNUSED_RESULT
macro (gcc's warn_unused_result attribute
) on functions which return allocated data, to warn users of the library if they're not using the data, and thus leaking memory.
After a little research, I've found no real uses of that gcc attribute in this manner, which is rather surprising. Perhaps people think the effort of adding the attribute to all applicable functions is not worth it, given that 90% of the time, users of their libraries will correctly use the result of functions (if not free them)?
Anybody got any insights into the matter? I'll probably go ahead and add the attributes anyway, since there are definitely a few functions in libgdata which return allocated data when the user probably isn't expecting it.
I can tell you why: because gcc already warns about that by default 🙂
The Linux kernel actually uses it that way, but we call it __must_check (IIRC).
Mark: I've just checked, and I don't get warned about it if the attribute isn't there. What warning options are you using?
it's more commonly used to denote error checkings that must be done, I think the rationale is that if your function allocates something, then it probably doesn't make sense for the caller to ever to save the result, so this type of error is very rare.
My coworker wrote a dehydra script to check that all functions that return an error code have that attribute set, a similar thing can probably be done to constructors.
Probably not widely used due to laziness and/or just not knowing about the feature. Please do add it.