better leak debugging

This commit is contained in:
ehouse 2008-07-28 05:05:39 +00:00
parent 1452e12650
commit db3fb12891

View file

@ -23,6 +23,8 @@
#include "comtypes.h" #include "comtypes.h"
#include "xwstream.h" #include "xwstream.h"
/* #define MPOOL_DEBUG */
#ifdef CPLUS #ifdef CPLUS
extern "C" { extern "C" {
#endif #endif
@ -92,12 +94,15 @@ checkIsText( MemPoolEntry* entry )
void void
mpool_destroy( MemPoolCtx* mpool ) mpool_destroy( MemPoolCtx* mpool )
{ {
if ( mpool->nUsed > 0 ) {
XP_WARNF( "leaking %d blocks", mpool->nUsed );
}
if ( !!mpool->usedList ) { if ( !!mpool->usedList ) {
MemPoolEntry* entry; MemPoolEntry* entry;
for ( entry = mpool->usedList; !!entry; entry = entry->next ) { for ( entry = mpool->usedList; !!entry; entry = entry->next ) {
#ifndef FOR_GREMLINS /* I don't want to hear about this right now */ #ifndef FOR_GREMLINS /* I don't want to hear about this right now */
XP_WARNF( "0x" XP_P " from ln %ld of %s\n", XP_LOGF( XP_P " from ln %ld of %s\n",
entry->ptr, entry->lineNo, entry->fileName ); entry->ptr, entry->lineNo, entry->fileName );
#ifdef DEBUG #ifdef DEBUG
{ {
char* tryTxt; char* tryTxt;
@ -144,6 +149,11 @@ mpool_alloc( MemPoolCtx* mpool, XP_U32 size, const char* file, XP_U32 lineNo )
++mpool->nUsed; ++mpool->nUsed;
++mpool->nAllocs; ++mpool->nAllocs;
#ifdef MPOOL_DEBUG
XP_LOGF( "%s(size=%ld,file=%s,lineNo=%ld)=>%p",
__func__, size, file, lineNo, entry->ptr );
#endif
return entry->ptr; return entry->ptr;
} /* mpool_alloc */ } /* mpool_alloc */
@ -198,6 +208,11 @@ mpool_free( MemPoolCtx* mpool, void* ptr, const char* file, XP_U32 lineNo )
file, lineNo ); file, lineNo );
} else { } else {
#ifdef MPOOL_DEBUG
XP_LOGF( "%s(ptr=%p):size=%ld,file=%s,lineNo=%ld)", __func__,
entry->ptr, entry->size, entry->fileName, entry->lineNo );
#endif
if ( !!prev ) { if ( !!prev ) {
prev->next = entry->next; prev->next = entry->next;
} else { } else {
@ -230,13 +245,21 @@ mpool_stats( MemPoolCtx* mpool, XWStreamCtxt* stream )
"Number of free blocks: %d\n" "Number of free blocks: %d\n"
"Total number of blocks allocated: %d\n", "Total number of blocks allocated: %d\n",
mpool->nUsed, mpool->nFree, mpool->nAllocs ); mpool->nUsed, mpool->nFree, mpool->nAllocs );
stream_putString( stream, buf ); if ( !!stream ) {
stream_putString( stream, buf );
} else {
XP_LOGF( buf );
}
for ( entry = mpool->usedList; !!entry; entry = entry->next ) { for ( entry = mpool->usedList; !!entry; entry = entry->next ) {
XP_SNPRINTF( buf, sizeof(buf), XP_SNPRINTF( buf, sizeof(buf),
(XP_UCHAR*)"%ld byte block allocated %s: line %ld\n", (XP_UCHAR*)"%ld byte block allocated at %p, %s: line %ld\n",
entry->size, entry->fileName, entry->lineNo ); entry->size, entry->ptr, entry->fileName, entry->lineNo );
stream_putString( stream, buf ); if ( !!stream ) {
stream_putString( stream, buf );
} else {
XP_LOGF( buf );
}
} }
} /* mpool_stats */ } /* mpool_stats */