I just write a small MemoryHook library with c++ and use as preload.But when I run the test every time,there is a large amount of memory allocated,the size is 72704. The usage is like this: LD_PRELOAD=./libPreload.so ./XXX
I try to find all related library in ldd list, but no size is 72704.I removed other information and only kept the size, but also has this problem.
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;
}
When I test a demo problem just has empty main function.I expect not memory unfree but the real output is:
++++++++++++++ unfreed addr: 0x55cc4ae22260, size: 72704, serial: 1 ++++++++++++++
backtrace:
./libPreLoad.so(_ZN11MemoryTrace13MemoryManager14storeBacktraceEPNS0_11tagUnitNodeE+0x28)[0x7f62fd6cceba]
./libPreLoad.so(_ZN11MemoryTrace13MemoryManager10appendUnitEPvmb+0x9f)[0x7f62fd6ccbb7]
./libPreLoad.so(_ZN11MemoryTrace10_impMallocEmb+0x52)[0x7f62fd6cd4cb]
./libPreLoad.so(_ZN11MemoryTrace11TraceMallocEm+0x58)[0x7f62fd6cd286]
./libPreLoad.so(malloc+0x18)[0x7f62fd6cc812]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x8f416)[0x7f62fd1cd416]
/lib64/ld-linux-x86-64.so.2(+0x10733)[0x7f62fd9e0733]
/lib64/ld-linux-x86-64.so.2(+0x10ca)[0x7f62fd9d10ca]
++++++++++++++ end ++++++++++++++
With no backtrace, the output is:
++++++++++++++ unfreed addr: 0x55f799c8e260, size: 72704, serial: 0 ++++++++++++++
backtrace:
++++++++++++++ end ++++++++++++++
And there is an additional problem that cannot count the memory release of static global variables.I can't find a suitable time to do this,even with attribute ((destructor(101))).