removed more memory leaks and improve btAlignedAllocator memory-leak debugging

This commit is contained in:
Erwin Coumans
2016-07-16 14:58:11 -07:00
parent bbefc6b108
commit 2caa2b7ff4
3 changed files with 40 additions and 12 deletions

Binary file not shown.

View File

@@ -207,7 +207,7 @@ void ImportUrdfSetup::initPhysics()
BulletURDFImporter u2b(m_guiHelper, 0); BulletURDFImporter u2b(m_guiHelper, 0);
bool loadOk = u2b.loadURDF(m_fileName); bool loadOk = u2b.loadURDF(m_fileName);
#ifdef TEST_MULTIBODY_SERIALIZATION #ifdef TEST_MULTIBODY_SERIALIZATION
//test to serialize a multibody to disk or shared memory, with base, link and joint names //test to serialize a multibody to disk or shared memory, with base, link and joint names
@@ -238,7 +238,11 @@ void ImportUrdfSetup::initPhysics()
m_data->m_rb = creation.getRigidBody(); m_data->m_rb = creation.getRigidBody();
m_data->m_mb = creation.getBulletMultiBody(); m_data->m_mb = creation.getBulletMultiBody();
btMultiBody* mb = m_data->m_mb; btMultiBody* mb = m_data->m_mb;
for (int i = 0; i < u2b.getNumAllocatedCollisionShapes(); i++)
{
m_collisionShapes.push_back(u2b.getAllocatedCollisionShape(i));
}
if (m_useMultiBody && mb ) if (m_useMultiBody && mb )
{ {
std::string* name = new std::string(u2b.getLinkName(u2b.getRootLinkIndex())); std::string* name = new std::string(u2b.getLinkName(u2b.getRootLinkIndex()));

View File

@@ -108,6 +108,18 @@ void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
//this generic allocator provides the total allocated number of bytes //this generic allocator provides the total allocated number of bytes
#include <stdio.h> #include <stdio.h>
struct btDebugPtrMagic
{
union
{
void** vptrptr;
void* vptr;
int* iptr;
char* cptr;
};
};
void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename) void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename)
{ {
void *ret; void *ret;
@@ -117,17 +129,24 @@ void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filen
gNumAlignedAllocs++; gNumAlignedAllocs++;
real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1)); int sz2prt = 2*sizeof(void *);
real = (char *)sAllocFunc(size + sz2prt + (alignment-1));
if (real) { if (real) {
ret = (void*) btAlignPointer(real + 2*sizeof(void *), alignment);
*((void **)(ret)-1) = (void *)(real); ret = (void*) btAlignPointer(real + sz2prt, alignment);
*((int*)(ret)-2) = size; btDebugPtrMagic p;
p.vptr = ret;
p.cptr-=sizeof(void*);
*p.vptrptr = (void*)real;
p.cptr-=sizeof(void*);
*p.iptr = size;
} else { } else {
ret = (void *)(real);//?? ret = (void *)(real);//??
} }
printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size); printf("allocation#%d at address %x, from %s,line %d, size %d (total allocated = %d)\n",gNumAlignedAllocs,real, filename,line,size,gTotalBytesAlignedAllocs);
int* ptr = (int*)ret; int* ptr = (int*)ret;
*ptr = 12; *ptr = 12;
@@ -141,16 +160,21 @@ void btAlignedFreeInternal (void* ptr,int line,char* filename)
gNumAlignedFree++; gNumAlignedFree++;
if (ptr) { if (ptr) {
real = *((void **)(ptr)-1); btDebugPtrMagic p;
int size = *((int*)(ptr)-2); p.vptr = ptr;
gTotalBytesAlignedAllocs -= size; p.cptr-=sizeof(void*);
real = *p.vptrptr;
p.cptr-=sizeof(void*);
int size = *p.iptr;
gTotalBytesAlignedAllocs -= size;
printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size); printf("free #%d at address %x, from %s,line %d, size %d (total remain = %d\n",gNumAlignedFree,real, filename,line,size, gTotalBytesAlignedAllocs);
sFreeFunc(real); sFreeFunc(real);
} else } else
{ {
printf("NULL ptr\n"); //printf("deleting a NULL ptr, no effect\n");
} }
} }