http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54202
Bug #: 54202 Summary: Overeager warning about freeing non-heap objects Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: thi...@kde.org GCC 4.7 has a warning about freeing non-heap objects that is way too eager. Compiling the following code in C or C++: ====== #include <stdlib.h> typedef struct Data { int refcount; } Data; extern const Data shared_null; Data *allocate() { return (Data *)(&shared_null); } void dispose(Data *d) { if (d->refcount == 0) free(d); } void f() { Data *d = allocate(); dispose(d); } ==== Produces the following warning: test.c: In function 'f' test.c:17:13: warning: attempt to free a non-heap object 'shared_null' [-Wfree-nonheap-object] The warning is overeager because it says "attempt to free" without indicating that it's only a possibility. GCC cannot prove that the call to free() will happen with that particular pointer, as the value of shared_null.refcount is not known. The warning should either: a) be modified to indicate it's only a possibility and the compiler can't prove it; b) be issued only when the compiler is sure that the free will happen on non-heap objects. Or both, by having two warnings: one for when it's sure and one for when it isn't.