How to avoid unexpected memory allocation

邓道宽 <[email protected]>
Newsgroups gmane.comp.debugging.valgrind
Message-ID <CAFCHFAZf5E2TSxmCVgnMudbcOS67Ou0dAAWVf_=U+=8oM4jdKw@mail.gmail.com>
Hi all
I just write a small MemoryHook library with c++ and use as preload.But
every time I start running, there is a big memory allocation,the size is
72704.I try to find all related library in ldd  list, but no size is 72704.
The usage is like this:
    LD_PRELOAD=./libPreload.so ./XXX

My initialize code:
*    static void TraceInitialize()*


























*    {#ifdef _DEBUG        fprintf( stderr, "call TraceInitialize\n" );
 #endif        pthread_mutex_lock( &s_mutexInit );        if ( s_status ==
TS_INITIALIZED ) { pthread_mutex_unlock( &s_mutexInit ); return; }
s_status = TS_INITIALIZING;        MemoryManager::initialize();
  s_pRealMalloc       = (FUNC_MALLOC)dlsym(RTLD_NEXT, "malloc");
s_pRealCalloc       = (FUNC_CALLOC)dlsym(RTLD_NEXT, "calloc");
s_pRealRealloc      = (FUNC_REALLOC)dlsym(RTLD_NEXT, "realloc");
s_pRealMemalign     = (FUNC_MEMALIGN)dlsym(RTLD_NEXT, "memalign");
s_pRealValloc       = (FUNC_VALLOC)dlsym(RTLD_NEXT, "valloc");
s_pRealFree         = (FUNC_FREE)dlsym(RTLD_NEXT, "free");        assert(
!( NULL == s_pRealMalloc || NULL == s_pRealCalloc || NULL == s_pRealRealloc
||                     NULL == s_pRealMemalign || NULL == s_pRealValloc ||
NULL == s_pRealFree ) );               s_status = TS_INITIALIZED;       //
printMap();               pthread_mutex_unlock( &s_mutexInit );    }*

and trace malloc call:
*     static int s_no_hook = 0;*









*     void* TraceMalloc( size_t size )     {            if ( s_status ==
TS_INITIALIZING ) return mockMemory::_mockMalloc( size );          if (
s_status != TS_INITIALIZED )  TraceInitialize();          void* p =
_impMalloc( size, __sync_fetch_and_add( &s_no_hook, 1 ) );
__sync_fetch_and_sub( &s_no_hook, 1 );          return p;      }*

And there is an additional problem that can't count the memory release of
static global variables.I can't find a suitable time to do this,even with
__attribute__ ((destructor(101))).

How can I fix these two bugs?

Complete code link:
          https://github.com/ddkv587/MemoryHook

_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.