我只是用c ++编写了一个小的MemoryHook库,并用作预加载。但是每次运行测试时,分配了大量的内存,大小为72704。 用法是这样的: LD_PRELOAD =。/ libPreload.so ./XXX
我尝试在ldd列表中找到所有相关的库,但是没有大小为72704。我删除了其他信息,仅保留了大小,但是也有此问题。
我的初始化代码:
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 );
}
并跟踪malloc调用:
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;
}
当我测试一个演示问题时,它的主要功能是空的。我期望内存不会释放,但是实际输出是:
++++++++++++++ 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 ++++++++++++++
没有回溯,输出为:
++++++++++++++ unfreed addr: 0x55f799c8e260, size: 72704, serial: 0 ++++++++++++++
backtrace:
++++++++++++++ end ++++++++++++++
还有一个其他问题,无法计算静态全局变量的内存释放。即使使用 attribute ((destructor(101))),我也找不到合适的时间来执行此操作