box2d.js icon indicating copy to clipboard operation
box2d.js copied to clipboard

Memory leak

Open timmyL17 opened this issue 7 years ago • 5 comments

Hi,

I'm seeing an issue where I think world.DestroyBody does not completely clean everything up. I've created a jsfiddle here showing it: https://jsfiddle.net/otx84Lfb/12/

There is a debugging function in there that figures out how much memory is left on the heap. When creating b2BodyDef objects and destroying them, the available memory is exactly the same. After creating and destroying b2Body objects using the world.DestroyBody function, some memory is lost. In our application, this causes the emscripten heap to eventually fill up.

Is there something else I'm supposed to do when destroying a body? Thanks.

timmyL17 avatar Jun 14 '17 17:06 timmyL17

Two updates:

  1. This also happens with version 2.3.1 (https://jsfiddle.net/otx84Lfb/13).

  2. It appears to be due to the small block allocator. If I recompile after changing Allocate in b2BlockAllocator.cpp to just return b2Alloc(size), and Free in b2BlockAllocator.cpp to just return b2Free(p), then the memory leak goes away.

timmyL17 avatar Jun 19 '17 22:06 timmyL17

@timmyL17 perhaps you should report this to https://github.com/erincatto/Box2D.

I believe you are referencing this line: https://github.com/erincatto/Box2D/blob/master/Box2D/Box2D/Common/b2BlockAllocator.cpp#L106

calvinte avatar Feb 04 '18 20:02 calvinte

Was this fixed?

soylomass avatar Jan 27 '20 15:01 soylomass

I'm having the same issue... is there an update?

joex92 avatar Nov 03 '20 01:11 joex92

Tried this with box2d-wasm (branch: memory-leak-repro) against Box2D v2.4.1 sources, and I encounter the same memory leak:

mem start:
11411808

mem after body def destroy:
11411808

mem after destroy bodies__:
11395384

count:
0

items:
0

The library distribution against which I tested is here (only thing different compared to the npm release is I configured ALLOW_MEMORY_GROWTH=0 in order for the checkMemory function in this repro to work correctly): box2d-wasm-1.0.2.tar.gz

Birch-san avatar Nov 04 '20 18:11 Birch-san