OimoPhysics icon indicating copy to clipboard operation
OimoPhysics copied to clipboard

BvhBroadPhase.hx - Memory leak

Open Snky opened this issue 1 year ago • 1 comments

Proxies added to movedProxies are not being removed when calling destroyProxy.

I've written this at the bottom of destroyProxy but I feel like the array needs sorting, as well as, bvhProxy._id is counting up at a different rate to numMovedProxies I believe:

movedProxies[bvhProxy._id] = null; numMovedProxies--; _idCount--;

Snky avatar Apr 24 '23 21:04 Snky

Alternatively I've now written: movedProxies[bvhProxy._id] = movedProxies[--numMovedProxies]; movedProxies[numMovedProxies] = null; _idCount--;

I thought I'd leave these here, and perhaps someone else can comment which is suitable, or if there's a better way.

Snky avatar May 06 '23 21:05 Snky

BvhBroadPhase.movedProxies is just a temporary array and will be cleared every step like in https://github.com/saharan/OimoPhysics/blob/c0c828a336c06380e90e92bb522f6344ce0f6cfa/src/oimo/collision/broadphase/bvh/BvhBroadPhase.hx#L217 or https://github.com/saharan/OimoPhysics/blob/c0c828a336c06380e90e92bb522f6344ce0f6cfa/src/oimo/collision/broadphase/bvh/BvhBroadPhase.hx#L197.

Destroying a proxy sets BvhProxy._moved to false so that the proxy won't be checked again and will just be discarded here: https://github.com/saharan/OimoPhysics/blob/c0c828a336c06380e90e92bb522f6344ce0f6cfa/src/oimo/collision/broadphase/bvh/BvhBroadPhase.hx#L208-L217

So it won't cause a memory leak. Also, the purpose of the ID assigned to each object is to distinguish between objects for debugging, and is not to store an index of an array. So this code

movedProxies[bvhProxy._id] = movedProxies[--numMovedProxies];

can access outside the array or overwrite and destroy necessary data to detect potential collisions.

saharan avatar May 17 '24 09:05 saharan

Oh, will have to get my head around why I thought that then, so cool to see you're back, I think having an easy way to change center of gravity (one of the other issues) will be a huge game changer for Oimo!

Snky avatar May 30 '24 20:05 Snky