From c54a61b97a98e770cf95752c00008a7eec933a1a Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Sat, 16 Jul 2016 21:29:31 -0700 Subject: [PATCH] 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) --- .../GwenGUISupport/GraphingTexture.cpp | 1 + .../ExampleBrowser/OpenGLExampleBrowser.cpp | 4 + examples/ExampleBrowser/main.cpp | 8 +- .../InverseDynamicsExample.cpp | 9 +-- .../PhysicsServerCommandProcessor.cpp | 4 + .../PhysicsServerSharedMemory.cpp | 1 - .../TinyRendererVisualShapeConverter.cpp | 11 ++- src/LinearMath/btAlignedAllocator.cpp | 78 +++++++++++++++++-- src/LinearMath/btAlignedAllocator.h | 6 ++ 9 files changed, 104 insertions(+), 18 deletions(-) diff --git a/examples/ExampleBrowser/GwenGUISupport/GraphingTexture.cpp b/examples/ExampleBrowser/GwenGUISupport/GraphingTexture.cpp index 4cfcc8f10..7a87be819 100644 --- a/examples/ExampleBrowser/GwenGUISupport/GraphingTexture.cpp +++ b/examples/ExampleBrowser/GwenGUISupport/GraphingTexture.cpp @@ -21,6 +21,7 @@ void GraphingTexture::destroy() m_height=0; glDeleteTextures(1,(GLuint*)&m_textureId); m_textureId=0; + } bool GraphingTexture::create(int texWidth, int texHeight) diff --git a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp index 18578c71a..e06d5f205 100644 --- a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp +++ b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp @@ -675,7 +675,10 @@ struct QuickCanvas : public Common2dCanvasInterface virtual void destroyCanvas(int canvasId) { btAssert(canvasId>=0); + delete m_gt[canvasId]; + m_gt[canvasId] = 0; destroyTextureWindow(m_gw[canvasId]); + m_gw[canvasId] = 0; m_curNumGraphWindows--; } virtual void setPixel(int canvasId, int x, int y, unsigned char red, unsigned char green,unsigned char blue, unsigned char alpha) @@ -745,6 +748,7 @@ OpenGLExampleBrowser::~OpenGLExampleBrowser() delete m_internalData; + gFileImporterByExtension.clear(); gAllExamples = 0; } diff --git a/examples/ExampleBrowser/main.cpp b/examples/ExampleBrowser/main.cpp index 7a155d6c0..f5b05211c 100644 --- a/examples/ExampleBrowser/main.cpp +++ b/examples/ExampleBrowser/main.cpp @@ -15,7 +15,7 @@ #include "../Importers/ImportURDFDemo/ImportURDFSetup.h" #include "../Importers/ImportSDFDemo/ImportSDFSetup.h" #include "../Importers/ImportSTLDemo/ImportSTLSetup.h" - +#include "LinearMath/btAlignedAllocator.h" int main(int argc, char* argv[]) @@ -49,5 +49,11 @@ int main(int argc, char* argv[]) delete exampleBrowser; } + +//#ifdef BT_DEBUG_MEMORY_ALLOCATIONS + int numBytesLeaked = btDumpMemoryLeaks(); + btAssert(numBytesLeaked==0); +//#endif//BT_DEBUG_MEMORY_ALLOCATIONS + return 0; } diff --git a/examples/InverseDynamics/InverseDynamicsExample.cpp b/examples/InverseDynamics/InverseDynamicsExample.cpp index 986f9024b..ebf7efb0d 100644 --- a/examples/InverseDynamics/InverseDynamicsExample.cpp +++ b/examples/InverseDynamics/InverseDynamicsExample.cpp @@ -73,7 +73,6 @@ class InverseDynamicsExample : public CommonMultiBodyBase { btInverseDynamicsExampleOptions m_option; btMultiBody* m_multiBody; - btAlignedObjectArray m_allocatedShapes; btInverseDynamics::MultiBodyTree *m_inverseModel; TimeSeriesCanvas* m_timeSeriesCanvas; public: @@ -107,12 +106,6 @@ InverseDynamicsExample::InverseDynamicsExample(struct GUIHelperInterface* helper InverseDynamicsExample::~InverseDynamicsExample() { - delete m_multiBody; - for (int i = 0; i < m_allocatedShapes.size(); i++) - { - delete m_allocatedShapes[i]; - } - m_allocatedShapes.resize(0); delete m_inverseModel; delete m_timeSeriesCanvas; } @@ -175,7 +168,7 @@ void InverseDynamicsExample::initPhysics() ConvertURDF2Bullet(u2b,creation, identityTrans,m_dynamicsWorld,true,u2b.getPathPrefix()); for (int i = 0; i < u2b.getNumAllocatedCollisionShapes(); i++) { - m_allocatedShapes.push_back(u2b.getAllocatedCollisionShape(i)); + m_collisionShapes.push_back(u2b.getAllocatedCollisionShape(i)); } m_multiBody = creation.getBulletMultiBody(); if (m_multiBody) diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 3f08f88e2..5c82e3381 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -31,6 +31,10 @@ struct UrdfLinkNameMapUtil UrdfLinkNameMapUtil():m_mb(0),m_memSerializer(0) { } + virtual ~UrdfLinkNameMapUtil() + { + delete m_memSerializer; + } }; diff --git a/examples/SharedMemory/PhysicsServerSharedMemory.cpp b/examples/SharedMemory/PhysicsServerSharedMemory.cpp index 0d29fae1e..d0b583969 100644 --- a/examples/SharedMemory/PhysicsServerSharedMemory.cpp +++ b/examples/SharedMemory/PhysicsServerSharedMemory.cpp @@ -78,7 +78,6 @@ PhysicsServerSharedMemory::PhysicsServerSharedMemory(SharedMemoryInterface* shar } m_data->m_commandProcessor = new PhysicsServerCommandProcessor; - m_data->m_commandProcessor ->createEmptyDynamicsWorld(); } diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp index 92bca7498..9457652a6 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp @@ -701,7 +701,16 @@ void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixels void TinyRendererVisualShapeConverter::resetAll() { - //todo: free memory + for (int i=0;im_swRenderInstances.size();i++) + { + TinyRendererObjectArray** ptrptr = m_data->m_swRenderInstances.getAtIndex(i); + if (ptrptr && *ptrptr) + { + TinyRendererObjectArray* ptr = *ptrptr; + delete ptr; + } + } + m_data->m_swRenderInstances.clear(); } diff --git a/src/LinearMath/btAlignedAllocator.cpp b/src/LinearMath/btAlignedAllocator.cpp index 3209b86ee..e5f6040c4 100644 --- a/src/LinearMath/btAlignedAllocator.cpp +++ b/src/LinearMath/btAlignedAllocator.cpp @@ -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 + +int btDumpMemoryLeaks() +{ + int totalLeak = 0; + + for (int i=0;i @@ -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