remove some memory leaks in example code.

This commit is contained in:
Erwin Coumans
2017-01-10 14:57:16 -08:00
parent 5e948ebe00
commit 3d6584962a
18 changed files with 103 additions and 38 deletions

View File

@@ -34,6 +34,8 @@ template <typename T, typename U> void addJointInfoFromMultiBodyData(const T* mb
{
{
b3JointInfo info;
info.m_jointName = 0;
info.m_linkName = 0;
info.m_flags = 0;
info.m_jointIndex = link;
info.m_qIndex =

View File

@@ -9,7 +9,7 @@
PhysicsClientSharedMemory2::PhysicsClientSharedMemory2(SharedMemoryCommandProcessor* proc)
:PhysicsDirect(proc)
:PhysicsDirect(proc,false)
{
m_proc = proc;
}

View File

@@ -8,7 +8,7 @@ b3PhysicsClientHandle b3ConnectSharedMemory2(int key)
SharedMemoryCommandProcessor* cmdProc = new SharedMemoryCommandProcessor();
cmdProc->setSharedMemoryKey(key);
PhysicsDirect* cl = new PhysicsDirect(cmdProc);
PhysicsDirect* cl = new PhysicsDirect(cmdProc,true);
cl->setSharedMemoryKey(key);

View File

@@ -10,7 +10,7 @@ b3PhysicsClientHandle b3ConnectPhysicsUDP(const char* hostName, int port)
UdpNetworkedPhysicsProcessor* udp = new UdpNetworkedPhysicsProcessor(hostName, port);
PhysicsDirect* direct = new PhysicsDirect(udp);
PhysicsDirect* direct = new PhysicsDirect(udp,true);
bool connected = direct->connect();
printf("direct!\n");

View File

@@ -62,11 +62,11 @@ struct PhysicsDirectInternalData
}
};
PhysicsDirect::PhysicsDirect(PhysicsCommandProcessorInterface* physSdk)
PhysicsDirect::PhysicsDirect(PhysicsCommandProcessorInterface* physSdk, bool passSdkOwnership)
{
m_data = new PhysicsDirectInternalData;
m_data->m_commandProcessor = physSdk;
m_data->m_ownsCommandProcessor = false;
m_data->m_ownsCommandProcessor = passSdkOwnership;
}
PhysicsDirect::~PhysicsDirect()
@@ -79,9 +79,40 @@ PhysicsDirect::~PhysicsDirect()
{
delete m_data->m_commandProcessor;
}
resetData();
delete m_data;
}
void PhysicsDirect::resetData()
{
m_data->m_debugLinesFrom.clear();
m_data->m_debugLinesTo.clear();
m_data->m_debugLinesColor.clear();
for (int i = 0; i<m_data->m_bodyJointMap.size(); i++)
{
BodyJointInfoCache2** bodyJointsPtr = m_data->m_bodyJointMap.getAtIndex(i);
if (bodyJointsPtr && *bodyJointsPtr)
{
BodyJointInfoCache2* bodyJoints = *bodyJointsPtr;
for (int j = 0; j<bodyJoints->m_jointInfo.size(); j++) {
if (bodyJoints->m_jointInfo[j].m_jointName)
{
free(bodyJoints->m_jointInfo[j].m_jointName);
}
if (bodyJoints->m_jointInfo[j].m_linkName)
{
free(bodyJoints->m_jointInfo[j].m_linkName);
}
}
delete (*bodyJointsPtr);
}
}
m_data->m_bodyJointMap.clear();
}
// return true if connection succesfull, can also check 'isConnected'
bool PhysicsDirect::connect()
@@ -525,6 +556,14 @@ bool PhysicsDirect::processCamera(const struct SharedMemoryCommand& orgCommand)
void PhysicsDirect::processBodyJointInfo(int bodyUniqueId, const SharedMemoryStatus& serverCmd)
{
BodyJointInfoCache2** cachePtr = m_data->m_bodyJointMap[bodyUniqueId];
//don't process same bodyUniqueId multiple times
if (cachePtr)
{
return;
}
bParse::btBulletFile bf(
&m_data->m_bulletStreamDataServerToClient[0],
serverCmd.m_numDataStreamBytes);
@@ -627,30 +666,7 @@ void PhysicsDirect::postProcessStatus(const struct SharedMemoryStatus& serverCmd
}
case CMD_RESET_SIMULATION_COMPLETED:
{
m_data->m_debugLinesFrom.clear();
m_data->m_debugLinesTo.clear();
m_data->m_debugLinesColor.clear();
for (int i = 0; i<m_data->m_bodyJointMap.size(); i++)
{
BodyJointInfoCache2** bodyJointsPtr = m_data->m_bodyJointMap.getAtIndex(i);
if (bodyJointsPtr && *bodyJointsPtr)
{
BodyJointInfoCache2* bodyJoints = *bodyJointsPtr;
for (int j = 0; j<bodyJoints->m_jointInfo.size(); j++) {
if (bodyJoints->m_jointInfo[j].m_jointName)
{
free(bodyJoints->m_jointInfo[j].m_jointName);
}
if (bodyJoints->m_jointInfo[j].m_linkName)
{
free(bodyJoints->m_jointInfo[j].m_linkName);
}
}
delete (*bodyJointsPtr);
}
}
m_data->m_bodyJointMap.clear();
resetData();
break;
}

View File

@@ -28,9 +28,10 @@ protected:
void postProcessStatus(const struct SharedMemoryStatus& serverCmd);
void resetData();
public:
PhysicsDirect(class PhysicsCommandProcessorInterface* physSdk);
PhysicsDirect(class PhysicsCommandProcessorInterface* physSdk, bool passSdkOwnership);
virtual ~PhysicsDirect();

View File

@@ -11,7 +11,7 @@ b3PhysicsClientHandle b3ConnectPhysicsDirect()
{
PhysicsServerCommandProcessor* sdk = new PhysicsServerCommandProcessor;
PhysicsDirect* direct = new PhysicsDirect(sdk);
PhysicsDirect* direct = new PhysicsDirect(sdk,true);
bool connected = direct->connect();
return (b3PhysicsClientHandle )direct;
}

View File

@@ -779,6 +779,7 @@ void PhysicsServerCommandProcessor::deleteDynamicsWorld()
for (int i=0;i<m_data->m_worldImporters.size();i++)
{
m_data->m_worldImporters[i]->deleteAllData();
delete m_data->m_worldImporters[i];
}
m_data->m_worldImporters.clear();

View File

@@ -573,7 +573,7 @@ public:
virtual ~MultiThreadedOpenGLGuiHelper()
{
delete m_childGuiHelper;
//delete m_childGuiHelper;
}
void setCriticalSection(b3CriticalSection* cs)
@@ -1152,9 +1152,12 @@ PhysicsServerExample::~PhysicsServerExample()
#ifdef BT_ENABLE_VR
delete m_tinyVrGui;
#endif
bool deInitializeSharedMemory = true;
m_physicsServer.disconnectSharedMemory(deInitializeSharedMemory);
m_isConnected = false;
delete m_multiThreadedHelper;
}
bool PhysicsServerExample::isConnected()
@@ -1247,6 +1250,11 @@ void PhysicsServerExample::exitPhysics()
printf("stopping threads\n");
m_threadSupport->deleteCriticalSection(m_args[0].m_cs);
m_threadSupport->deleteCriticalSection(m_args[0].m_cs2);
m_threadSupport->deleteCriticalSection(m_args[0].m_cs3);
m_threadSupport->deleteCriticalSection(m_args[0].m_csGUI);
delete m_threadSupport;
m_threadSupport = 0;

View File

@@ -82,7 +82,7 @@ public:
newargv[argc+1] = t1;
m_data = btCreateInProcessExampleBrowser(newargc,newargv);
SharedMemoryInterface* shMem = btGetSharedMemoryInterface(m_data);
free(newargv);
setSharedMemoryInterface(shMem);
}

View File

@@ -122,6 +122,7 @@ TinyRendererVisualShapeConverter::TinyRendererVisualShapeConverter()
}
TinyRendererVisualShapeConverter::~TinyRendererVisualShapeConverter()
{
resetAll();
delete m_data;
}
@@ -972,6 +973,13 @@ void TinyRendererVisualShapeConverter::resetAll()
if (ptrptr && *ptrptr)
{
TinyRendererObjectArray* ptr = *ptrptr;
if (ptr)
{
for (int o=0;o<ptr->m_renderObjects.size();o++)
{
delete ptr->m_renderObjects[o];
}
}
delete ptr;
}
}