进程退出共享内存不一定释放

如下程序,在backtrace (array, 10)中,申请5次内存,进程退出并没有立即释放。Valgrind检测结果如下:

[root@localhost memory]# valgrind --tool=memcheck ./sample
==6452== Memcheck, a memory error detector.
==6452== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==6452== Using LibVEX rev 1658, a library for dynamic binary translation.
==6452== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==6452== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==6452== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==6452== For more details, rerun with: -v
==6452== 
Obtained 5 stack frames.
==6452== 
==6452== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
==6452== malloc/free: in use at exit: 904 bytes in 5 blocks.
==6452== malloc/free: 6 allocs, 1 frees, 914 bytes allocated.
==6452== For counts of detected errors, rerun with: -v
==6452== searching for pointers to 5 not-freed blocks.
==6452== checked 51,772 bytes.
==6452== 
==6452== LEAK SUMMARY:
==6452==    definitely lost: 0 bytes in 0 blocks.
==6452==      possibly lost: 0 bytes in 0 blocks.
==6452==    still reachable: 904 bytes in 5 blocks.
==6452==         suppressed: 0 bytes in 0 blocks.
==6452== Reachable blocks (those to which a pointer was found) are not shown.
==6452== To see them, rerun with: --show-reachable=yes

 

#include
#include
#include
#include

static void* (* old_malloc_hook)(size_t,const void*);
static void (* old_free_hook)(void*,const void*);
static void my_init_hook(void);
static void* my_malloc_hook(size_t,const void*);
static void my_free_hook(void*,const void*);


static void my_init_hook(void)
{
        old_malloc_hook=__malloc_hook;
        old_free_hook=__free_hook;
        __malloc_hook=my_malloc_hook;
        __free_hook=my_free_hook;
}

static void* my_malloc_hook(size_t size,const void* caller)
{
        void *array[10];
        size_t size1;
        void *result;
        __malloc_hook=old_malloc_hook;
        __free_hook=old_free_hook;
//      size1 = backtrace (array, 10);
        __free_hook=my_free_hook;
        //printf ("Obtained %zd stack frames.\n", size1);
        result=malloc(size);
        //old_malloc_hook=__malloc_hook;
        __malloc_hook=my_malloc_hook;
        printf("@@@ %p + %p 0x%x",caller,result,(unsigned long int)size);
        int i=0;
/*      for(i=0;i */              printf(" %p",array[i]);
        printf("\n");
        return result;
}
static void my_free_hook(void* ptr,const void* caller)
{
        __free_hook=old_free_hook;
        free(ptr);
        old_free_hook=__free_hook;
        printf("@@@ %p - %p\n",caller,ptr);
        __free_hook=my_free_hook;
}

 

/* Obtain a backtrace and print it to stdout. */
void print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;

size = backtrace (array, 10);
strings = backtrace_symbols (array, size);

printf ("Obtained %zd stack frames.\n", size);

for (i = 0; i < size; i++)
printf ("%s\n", strings[i]);

free (strings);
}

/* A dummy function to make the backtrace more interesting. */
void dummy_function (void)
{
print_trace ();
}

int
main (void)
{
my_init_hook();
dummy_function ();
int *p=(int*)malloc(10);
free(p);
return 0;
}

先放着,还需继续研究。。。

分享到:        

你可能感兴趣的:(Memory)