char *buf __attribute__( ( cleanup( free ) ) ) = NULL;
void bug(void) {
char *buf __attribute__( ( cleanup( free ) ) ) = (char*)malloc(1);
}
void bug(void) {
char *buf __attribute__( ( cleanup( free ) ) ) = (char*)malloc(1);
}
int main( int argc, const char* argv[] )
{
char *test __attribute__( ( cleanup( free ) ) ) = (char*)malloc(1);
return 0;
Consider the following example of a bug() function:
The need to call free comes from using vasprintf, as it will automatically allocate (using malloc) a string to hold the resulting formatted string. The above function is pretty simplistic, and has only the one exit point, so having to specify a free() isn't so much of an issue there. However, I prefer to still use the cleanup attribute so that remembering to use free() at all future exit points becomes unnecessary. Anyhow, the above function works fine in g++ and doesn't produce any errors in valgrind.
However, the aforementioned error occurs when the code is written like this:
Is there something I'm missing about using the cleanup variable attribute in g++? Could valgrind just be giving me a false positive? I'm completely baffled at this point. :sad:
Also, before I forget, there are two other important points to address:
1) I know about streams. I don't want to use streams. The syntax is awkward to me, and not nearly as convenient as the printf-style format syntax.
2) I know about the Boost format library. I don't want to use that either, for the same reason as #1, among other reasons.