[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: STL, memory allocation, and cleanup (gcc 3.0?)



>>>>> "elf" == elf  <elf@florence.buici.com> writes:

    elf> I've written a C++ program to demonstrate behavior that I
    elf> cannot explain.  It appears that the STL allocates memory
    elf> that it never frees.  I'd like to believe that I'm missing
    elf> something obvious, but then it must not be obvious enough.
    elf> Yes, I've run the debugger on it.  It looks to be a GCC bug,
    elf> but I cannot be sure since I don't have an explanation for
    elf> the fact that a large block is allocated for the first Map
    elf> and not for the second, *and* there are no static members in
    elf> the Map template.

    elf> The program hooks new/delete and malloc/free, printing
    elf> messages when one is called.  This all arose because I am
    elf> using nearly identical code in another application to prove
    elf> that it is memory conservative.

I get:

map_p
malloc 12 -> 0x8053808
malloc 960 -> 0x8053818
free 0x8053808
map_p
malloc 12 -> 0x8053808
free 0x8053808

This looks like (to me anyway) that 960 bytes memory is getting
allocated by STL, and it is allocated static so that it gets shared by
subsequent calls by whatever uses it.

    elf> Questions include:

    elf>   1) Is there a gcc 3.0 machine I can use to run this test?

I got compile errors. Sorry, I don't know what is wrong either :-)

[521] [scrooge:bam] ~ >gcc-3.0 -o freetest freetest.cc      
freetest.cc:24: syntax error before `;' token
freetest.cc:25: syntax error before `;' token
freetest.cc:30: 'Map' is used as a type, but is not defined as a type.
freetest.cc: In function `void test_list()':
freetest.cc:78: `list' undeclared (first use this function)
freetest.cc:78: (Each undeclared identifier is reported only once for each 
   function it appears in.)
freetest.cc:78: parse error before `*' token
freetest.cc: In function `void test_list_p()':
freetest.cc:83: parse error before `*' token
freetest.cc:84: `pl' undeclared (first use this function)
freetest.cc: In function `void test_map()':
freetest.cc:89: `Map' undeclared (first use this function)
freetest.cc:89: parse error before `;' token
freetest.cc:90: `m' undeclared (first use this function)
freetest.cc:90: `Pair' undeclared (first use this function)
freetest.cc: In function `void test_map_p()':
freetest.cc:95: `pm' undeclared (first use this function)
freetest.cc:95: parse error before `;' token

suggestion: compile with -Wall. This will catch errors like:

freetest.cc: In function `void * _malloc(unsigned int)':
freetest.cc:41: warning: unsigned int format, pointer arg (arg 3)
freetest.cc: In function `void _free(void *)':
freetest.cc:47: warning: unsigned int format, pointer arg (arg 2)

(sorry about messing up the formatting...)

    elf> void* _malloc (size_t cb) { void* pv = malloc (cb); printf
    elf> ("malloc %d -> 0x%x\n", cb, pv); return pv; }

    elf> void _free (void* pv) { printf ("free 0x%x\n", pv); free
    elf> (pv); }

pointers should be %p, not %x. This will avoid the warning. When you
use %p, you don't need to append the 0x at the front, it will happen
automatically.
-- 
Brian May <bam@debian.org>



Reply to: