I think that GCC extension __attribute__((cleanup))
is a good idea, at least for some cases, but i can't figure out how to use it in a good way. All i'm doing looks still really annoying.
I saw a lot of code doing #define _cleanup_(x) __attribute__((cleanup(x))
just to type less, but it there a way to pass there a standard function like free
or closedir
, fclose
, etc?
As I see I can't just write:
__attribute__((cleanup(free))) char *foo = malloc(10);
Because the cleanup callback will receive char**
pointer, and I have to always write something like:
static void free_char(char **ptr) { free(*ptr); }
__cleanup__((free_char)) char *foo = malloc(10);
That's pretty annoying, and the most annoying part is to define such cleanup functions for all types you need, because obviously you can't just define it for void **
. What is the best way to avoid these things?