http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46181
Summary: Feature request: "free-like" attribute Product: gcc Version: unknown Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: m.j.tha...@googlemail.com It would be nice for purposes of static error checking to be able to mark functions as destructors for some resource. Specifically, that a if a value is passed from a variable to argument n of the function then it is no longer valid after the call to the function. Optionally with an "invalid" value that may be assigned to the variable afterwards. E.g. int close(int fd) __attribute__(( free(1, -1) )); /* Says that the value passed to the first argument is invalid hereafter, and that the variable it came from can be marked invalid by setting it to -1 */ So that the compiler can see that the following is bad: int fd = open("filename", O_CREAT); ... if (close(fd) < 0) { ... } do_something_with(fd); but the following might be alright: int fd = open("filename", O_CREAT); ... if (close(fd) < 0) { ... } fd = -1; do_something_with(fd); It might also make sense to be able to tag a typedef as a resource type with information about a free-like function and an invalid value: typedef int myfile __attribute(( resource(close, -1) ));