More example code is memory-leak free now, in particular PhysicsServerExample.

/PhysicsServerCommandProcessor
also fixed some memory issue in InverseDynamicsExample (the base class is supposed to delete collision shape memory)
This commit is contained in:
Erwin Coumans
2016-07-16 21:29:31 -07:00
parent 589fa376b3
commit c54a61b97a
9 changed files with 104 additions and 18 deletions

View File

@@ -105,6 +105,27 @@ void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
}
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
static int allocations_id[10241024];
static int allocations_bytes[10241024];
static int mynumallocs = 0;
#include <stdio.h>
int btDumpMemoryLeaks()
{
int totalLeak = 0;
for (int i=0;i<mynumallocs;i++)
{
printf("Error: leaked memory of allocation #%d (%d bytes)\n", allocations_id[i], allocations_bytes[i]);
totalLeak+=allocations_bytes[i];
}
if (totalLeak)
{
printf("Error: memory leaks: %d allocations were not freed and leaked together %d bytes\n",mynumallocs,totalLeak);
}
return totalLeak;
}
//this generic allocator provides the total allocated number of bytes
#include <stdio.h>
@@ -122,32 +143,56 @@ struct btDebugPtrMagic
void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename)
{
if (size==0)
{
printf("Whaat? size==0");
return 0;
}
static int allocId = 0;
void *ret;
char *real;
// to find some particular memory leak, you could do something like this:
// if (allocId==172)
// {
// printf("catch me!\n");
// }
// if (size>1024*1024)
// {
// printf("big alloc!%d\n", size);
// }
gTotalBytesAlignedAllocs += size;
gNumAlignedAllocs++;
int sz2prt = 2*sizeof(void *);
int sz4prt = 4*sizeof(void *);
real = (char *)sAllocFunc(size + sz2prt + (alignment-1));
real = (char *)sAllocFunc(size + sz4prt + (alignment-1));
if (real) {
ret = (void*) btAlignPointer(real + sz2prt, alignment);
ret = (void*) btAlignPointer(real + sz4prt, alignment);
btDebugPtrMagic p;
p.vptr = ret;
p.cptr-=sizeof(void*);
*p.vptrptr = (void*)real;
p.cptr-=sizeof(void*);
*p.iptr = size;
p.cptr-=sizeof(void*);
*p.iptr = allocId;
allocations_id[mynumallocs] = allocId;
allocations_bytes[mynumallocs] = size;
mynumallocs++;
} else {
ret = (void *)(real);//??
}
printf("allocation#%d at address %x, from %s,line %d, size %d (total allocated = %d)\n",gNumAlignedAllocs,real, filename,line,size,gTotalBytesAlignedAllocs);
printf("allocation %d at address %x, from %s,line %d, size %d (total allocated = %d)\n",allocId,real, filename,line,size,gTotalBytesAlignedAllocs);
allocId++;
int* ptr = (int*)ret;
*ptr = 12;
return (ret);
@@ -157,19 +202,38 @@ void btAlignedFreeInternal (void* ptr,int line,char* filename)
{
void* real;
gNumAlignedFree++;
if (ptr) {
gNumAlignedFree++;
btDebugPtrMagic p;
p.vptr = ptr;
p.cptr-=sizeof(void*);
real = *p.vptrptr;
p.cptr-=sizeof(void*);
int size = *p.iptr;
p.cptr-=sizeof(void*);
int allocId = *p.iptr;
bool found = false;
for (int i=0;i<mynumallocs;i++)
{
if ( allocations_id[i] == allocId)
{
allocations_id[i] = allocations_id[mynumallocs-1];
allocations_bytes[i] = allocations_bytes[mynumallocs-1];
mynumallocs--;
found = true;
break;
}
}
gTotalBytesAlignedAllocs -= size;
printf("free #%d at address %x, from %s,line %d, size %d (total remain = %d\n",gNumAlignedFree,real, filename,line,size, gTotalBytesAlignedAllocs);
int diff = gNumAlignedAllocs-gNumAlignedFree;
printf("free %d at address %x, from %s,line %d, size %d (total remain = %d in %d non-freed allocations)\n",allocId,real, filename,line,size, gTotalBytesAlignedAllocs, diff);
sFreeFunc(real);
} else