From 2caa2b7ff407921eb84a459087fd71d8c1df84ce Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Sat, 16 Jul 2016 14:58:11 -0700 Subject: [PATCH] removed more memory leaks and improve btAlignedAllocator memory-leak debugging --- data/multibody.bullet | Bin 14584 -> 14584 bytes .../ImportURDFDemo/ImportURDFSetup.cpp | 8 +++- src/LinearMath/btAlignedAllocator.cpp | 44 ++++++++++++++---- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/data/multibody.bullet b/data/multibody.bullet index 1074b55dc5e60dafd11b59406edb7842bdfc57dd..31800308cedc7804a8642a0343c167373049f94b 100644 GIT binary patch delta 899 zcmb`GO=uHA6vt=cCYy#9p&zlDRBPRuR?XUKO>4|-x1NO7LJ?BLODc*WB8?X*u@>sV zOF`>gJt%tcRFEj1CIP{Z$_BgzPoBJZ^57xRLjAwV#E-iRznS^H_jYD>cDGS%6d%5_ zYC#}m!OF_-c5HonaV5?e+XH&RUa$`&KoazU{onxT2M56bI0PIp2o8f37y`qfbv<8H z6&kzXI2Zw=-~>1c#=t2GWiS{A6W}z+f-@io1n|Hl`27B?_u5%jG8xL9yN#!~GGAAS zvs)QguXJs`PW9|ejoY#9~W>BuIs;zc6(F#`jnV81qf6S zP;gzaksj9@+etSkgzJUjI&U_pWCLS~We{*?0dV$+=v#pfGg9&`i9{oAA3|tvk@`v3`1r(_K SRnWSD3gR*!oh|>2S?(`W)WyaC delta 743 zcmZ9K%S#(k7{%`-<0OvefoKF3i4sVaAOS%TjEUe%0xGy_wNA9HZ=KmG3g!=RQTe(R zM$E>?F1it-S(XyPZ8rtGsoG5t1Q%jFcdipP7k+c+JLlZ*-g!J2561W7s&2+uJEi1a zx^=1BkGkyN2{`FW4%HS*UMEbJxPB2bdhU!1eqW=N253yiX`9iFF!kyBDAJ6H$-U@ckN`=T@AKoF9^Q%!Fp&TT6b4XBer_&3{6_ z^t^vpMdY$UIVwrb+0iPWB(+gW3Cm~|r!6H!nyk{P(ke0ft%ToH9jX~U_=mkH6k?WIzVN=~Osq0P zraDF$_bTl>y!6N25-{_KbIRlJpfiR)sYwwhq>bt%6;+uE)vFU9k@^J2!36jWCV>W0 pU`*0W5+gunbm!4h-5=haH~H?47D~Jp&6(j}HI< diff --git a/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp b/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp index 33bc35d32..ebe8334a5 100644 --- a/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp +++ b/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp @@ -207,7 +207,7 @@ void ImportUrdfSetup::initPhysics() BulletURDFImporter u2b(m_guiHelper, 0); - bool loadOk = u2b.loadURDF(m_fileName); + bool loadOk = u2b.loadURDF(m_fileName); #ifdef TEST_MULTIBODY_SERIALIZATION //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_mb = creation.getBulletMultiBody(); 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 ) { std::string* name = new std::string(u2b.getLinkName(u2b.getRootLinkIndex())); diff --git a/src/LinearMath/btAlignedAllocator.cpp b/src/LinearMath/btAlignedAllocator.cpp index a65296c6a..3209b86ee 100644 --- a/src/LinearMath/btAlignedAllocator.cpp +++ b/src/LinearMath/btAlignedAllocator.cpp @@ -108,6 +108,18 @@ void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc) //this generic allocator provides the total allocated number of bytes #include +struct btDebugPtrMagic +{ + union + { + void** vptrptr; + void* vptr; + int* iptr; + char* cptr; + }; +}; + + void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename) { void *ret; @@ -117,17 +129,24 @@ void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filen gNumAlignedAllocs++; - real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1)); +int sz2prt = 2*sizeof(void *); + + real = (char *)sAllocFunc(size + sz2prt + (alignment-1)); if (real) { - ret = (void*) btAlignPointer(real + 2*sizeof(void *), alignment); - *((void **)(ret)-1) = (void *)(real); - *((int*)(ret)-2) = size; + + ret = (void*) btAlignPointer(real + sz2prt, alignment); + btDebugPtrMagic p; + p.vptr = ret; + p.cptr-=sizeof(void*); + *p.vptrptr = (void*)real; + p.cptr-=sizeof(void*); + *p.iptr = size; } else { 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; *ptr = 12; @@ -141,16 +160,21 @@ void btAlignedFreeInternal (void* ptr,int line,char* filename) gNumAlignedFree++; if (ptr) { - real = *((void **)(ptr)-1); - int size = *((int*)(ptr)-2); - gTotalBytesAlignedAllocs -= size; + btDebugPtrMagic p; + p.vptr = ptr; + 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); } else { - printf("NULL ptr\n"); + //printf("deleting a NULL ptr, no effect\n"); } }