Skip to content
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

C: cleanup attribute not supported #8

Open
phomes opened this issue Jun 11, 2015 · 4 comments
Open

C: cleanup attribute not supported #8

phomes opened this issue Jun 11, 2015 · 4 comments

Comments

@phomes
Copy link

phomes commented Jun 11, 2015

The cleanup attribute does not seem to be supported. I get many many false positives for resource and mem leaks due to this.

cleanup (cleanup_function)
The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.

If -fexceptions is enabled, then cleanup_function is run during the stack unwinding that happens during the processing of the exception. Note that the cleanup attribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html

@dulmarod
Copy link
Contributor

Hi,
Thanks for reporting this. We haven't looked into the cleanup attribute yet, but would be happy to support it. Could you please give us some small example of how this attribute is used and what kind of false positive leaks you get?
Thanks,
Dulma

@phomes
Copy link
Author

phomes commented Jun 12, 2015

Hi,
Here is an example of how to use it. When a variable with the attribute goes out of scope the function specified with the cleanup attribute will be called. The typical usecase is for free'ing the variable. This saves us from manually freeing in all error cases etc.

#include <stdlib.h>
#include <string.h>

void my_cleanup_func(char **c)
{
  puts("debug: cleanup_func called");
  free(*c);
}

int main(int argc, char **argv)
{
  __attribute__ ((cleanup(my_cleanup_func))) char *s = NULL;

  s = strdup("demo string");

  return 0; /* s goes out of scope. Cleanup function called - no leak */
}

@vavrusa
Copy link

vavrusa commented Jan 10, 2018

It'd be great to support this, as it can't be easily modeled by a function or a macro.

@iphydf
Copy link

iphydf commented Feb 24, 2018

This is equivalent to C++ scope bound resource management, i.e. a class with a destructor.

facebook-github-bot pushed a commit that referenced this issue Jun 19, 2018
Summary: Record that it is not supported: #8

Reviewed By: mbouaziz, dulmarod

Differential Revision: D8442762

fbshipit-source-id: fa271cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants