class TestCase {
private:
  const int *pointer;

public:
  TestCase() {
    static int non_heap = 0;
    this->pointer = &non_heap;
  }

  ~TestCase() {
    if (*this->pointer != 0) __builtin_free(const_cast<int *>(this->pointer));
  }
};

int main(int argc, const char *argv[]) {
 TestCase test;
 return 0;
}

/*

The code above, compiled g++ 4.4.0 with -O1 (or any -On except -O0) gives the
following warning:
test.cpp: In function ‘int main(int, const char**)’:
test.cpp:12: warning: attempt to free a non-heap object ‘non_heap’

In this case the warning is bogus because the "free" can never be reached.

Complete command line:
g++ -O1 -o test test.cpp

Output from "g++ -v -O1 -o test test.cpp":
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --with-arch=pentium-m --enable-threads
--enable-languages=c,c++
Thread model: posix
gcc version 4.4.0 20081210 (experimental) (GCC)
COLLECT_GCC_OPTIONS='-v' '-O1' '-o' 'test' '-shared-libgcc' '-mtune=generic'
'-march=pentium-m'
 /usr/local/libexec/gcc/i686-pc-linux-gnu/4.4.0/cc1plus -quiet -v -D_GNU_SOURCE
test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=pentium-m -auxbase
test -O1 -version -o /tmp/ccIffeq5.s
ignoring nonexistent directory
"/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../../include/c++/4.4.0

/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../../include/c++/4.4.0/i686-pc-linux-gnu

/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../../include/c++/4.4.0/backward
 /usr/local/include
 /usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/include
 /usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/include-fixed
 /usr/include
End of search list.
GNU C++ (GCC) version 4.4.0 20081210 (experimental) (i686-pc-linux-gnu)
        compiled by GNU C version 4.4.0 20081210 (experimental), GMP version
4.2.2, MPFR version 2.3.2.
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: b043f60097b3fa3e759e89265ea8ebdb
test.cpp: In function ‘int main(int, const char**)’:
test.cpp:12: warning: attempt to free a non-heap object ‘non_heap’
COLLECT_GCC_OPTIONS='-v' '-O1' '-o' 'test' '-shared-libgcc' '-mtune=generic'
'-march=pentium-m'
 as -V -Qy -o /tmp/ccshvEr5.o /tmp/ccIffeq5.s
GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU Binutils
for Debian) 2.18.0.20080103
COMPILER_PATH=/usr/local/libexec/gcc/i686-pc-linux-gnu/4.4.0/:/usr/local/libexec/gcc/i686-pc-linux-gnu/4.4.0/:/usr/local/libexec/gcc/i686-pc-linux-gnu/:/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/local/lib/gcc/i686-pc-linux-gnu/
LIBRARY_PATH=/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-O1' '-o' 'test' '-shared-libgcc' '-mtune=generic'
'-march=pentium-m'
 /usr/local/libexec/gcc/i686-pc-linux-gnu/4.4.0/collect2 --eh-frame-hdr -m
elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o test /usr/lib/crt1.o
/usr/lib/crti.o /usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/crtbegin.o
-L/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0
-L/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/../../.. /tmp/ccshvEr5.o -lstdc++
-lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/local/lib/gcc/i686-pc-linux-gnu/4.4.0/crtend.o /usr/lib/crtn.o


Note: this is a self-compiled binary of SVN revision 142654.
*/


-- 
           Summary: Bogus "attempt to free a non-heap object" warning
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rubidium at openttd dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38509

Reply via email to